The source for this, according to our Gemara, is a passage in Sefer Bamidbar (25:4) that teaches that after the Children of Israel engaged in sexual relations with the daughters of Moav, Moshe was commanded to punish the people who were involved by hanging them neged ha-shamesh – facing the sun – i.e. during the day. Having engaged in forbidden sexual relations as well as idol worship (see 25:2-3), the perpetrators were liable to receive a death penalty.
According to today’s Gemara, this command demands explanation, for Moshe was not told to punish the perpetrators, rather he was commanded to take kol rashei ha-am – the leaders of the people. The Gemara asks: If the people sinned, what was the responsibility of the leaders?
In response, Rav Yehuda quotes Rav as offering a radically different interpretation to the pasuk. According to Rav’s approach, the leaders were not to be punished, rather Moshe was commanded to assign them to play the role of judges and set up courts of law to try the people who sinned. The Gemara explains that the need for many courts to be established did not stem from a technical rule that forbids a court from judging two people on a given day, since Rav Ḥisda taught that many people can be tried on the same day if it is for the same offense. Rather the need for many courts was to “remove God’s anger” (as indicated in the closing words of Bamidbar 25:4).
Rashi’s explanation of this idea is that by setting up courts to judge these cases, God will see that the nation is working zealously to defend His honor and will therefore turn away His anger. The Ramah suggests that it would take a significant amount of time were a single court to try all of the cases, and as long as evildoers exist in the world, God’s anger is manifest. By establishing many courts the evildoers could be dealt with quickly, removing God’s anger.