“And Israel attached itself to Ba’al P’or, and G-d’s wrath raged against Israel. And G-d said to Moshe, ‘take all the leaders of the people and impale them in the presence of the sun’ so that the raging wrath of G-d against Israel will be reversed. And Moshe said to the judges of Israel, ‘each man shall kill his men, those who have become attached to Ba’al P’or.” A simple reading of these verses (Bamidbar 25:3-5) is not so simple.
The expression “in the presence of the sun” is unclear as well. It has been understood to mean several things, but since they are not mutually exclusive, was likely purposely made unclear so that all of them can be gleaned from the words. What each judge killing “his men” means is also unclear; why are they considered “his” men? Rashi says that every judge was given the task of killing two men; the two each was assigned to execute would therefore be considered “his men.” [It should be noted that this comment does not appear in the first printing of Rashi‘s commentary, although the Ramban does respond to it, quoting it as being Rashi’s words.] Although in the end these executions doesn’t seem to have occurred (as only the 24,000 who died in the plague are mentioned, see Ramban on 25:5), at this point it wasn’t known that Pinachas’ act would obviate the need to prosecute and then execute the guilty. Nevertheless, on a “p’shat” level, the term “his men” needs an explanation.
In the Midrashic literature (e.g. Tanchuma Balak 19/28, Bamidbar Rabbah 20:23), there is a dispute as to whether it is the sinnerswho are to be impaled, or the leaders for not trying to prevent the sinning. As previously mentioned, there are issues with both possibilities, as Moshe’s instructions to the judges clearly indicate that he understood G-d’s command to be to impale the sinners, not the leaders, while the wording of the instructions, when not taken in context, indicates that it is the leaders who are supposed to be impaled. The Talmud (Sanhedrin 35a) seems to recognize that the words indicate that the leaders are the ones who should be impaled, saying that this can’t be so, as they weren’t the ones who sinned. Instead, the Talmud understands these words to mean bringing the guilty into court to be prosecuted and (after following the proper court procedures) executed. The only context the Talmud seems concerned about is the injustice of impaling those who didn’t do anything, not whether Moshe had the ability to impale them (could he overpower all the other leaders and execute them? Why wasn’t he considered one of the leaders to be executed?) nor his instructions to the judges indicating that they weren’t the ones G-d wanted impaled. What about their guilt for not trying to prevent it? And why are the other contextual clues ignored?
The S’fornu says that the words “in the presence of the sun” mean “in broad daylight,” i.e. publicly, so that everyone is aware that the guilty are being brought to court. This way, those who did not actively try to prevent the sinning from occurring can make amends by not trying to prevent the prosecution and execution of those who had sinned. (This is similar to those who were guilty of not stopping others from worshipping the golden calf being given the chance to atone for it by not trying to stop the Levi’im from executing those who had.) This idea can be extended to the leaders as well.
The Talmud (Shabbos 55a) says that when G-d punishes sinners, the righteous are punished even before the sinners if they did not try to stop the others from sinning. “Kol Yisrael areivim zeh ba’zeh,” everyone is responsible for everyone else, and the responsibility to prevent sin falls on the leadership even more. Therefore, when the nation attached itself to Ba’al P’or, G-d was not only upset with the sinners, but with the leadership for not trying to prevent it. Nevertheless, there was a way to assuage G-d’s anger, by having those same leaders take an active part in prosecuting the guilty. G-d therefore told Moshe to take the leaders and have them start to prosecute the guilty (in broad daylight so that the laymen can achieve atonement too, by not trying to stop the prosecution). In order to make it clear that the leaders also deserved to be punished, the instructions to impale the guilty was phrased in a way that made it clear that the leaders deserved to be impaled too. Moshe therefore told the leaders to start prosecuting the guilty in order to reverse G-d’s wrath. And since the leaders were held responsible for not trying to prevent others from sinning, and could avoid punishment by prosecuting the sinners, the guilty were called “their men,” i.e. the men they were responsible for.
Although both groups, the ones who actually sinned and the leaders who didn’t do enough to prevent the sinning, were at least subtly included in the command to be impaled, the Midrashic dispute revolves around which meaning was primary, and which was secondary. Did G-d tell Moshe to impale the leaders, with the embedded message being that this can be avoided if they bring the sinners to justice, or did He tell Moshe (based on the context) that the leaders should bring the sinners to justice, because otherwise they deserved to be impaled themselves? The starting point of the Talmudic discussion was that the leaders deserved to be punished, so the verse could/should be referring to them being the ones to be impaled. However, since the leaders didn’t do anything (but were guilty of passively standing by while it happened), the Talmud then says that the guilt of the sinners must be mentioned before the punishment of the leaders can be (as it is in Shabbos). Therefore, the Talmud sides with the opinion in the Midrashim that the primary meaning of the verse refers to the sinners being impaled (not the leaders), albeit not because of the context, but because the actual sinners must be declared deserving of punishment before those who didn’t try to prevent the sinning can be.
The bottom line is that the Torah purposely embedded a double meaning into the verses, one based on the context and one based on the wording of the instructions to “impale them,” because the “them” could refer to the leaders, i.e. if they don’t bring the sinners they were responsible for to justice, or it could refer to the sinners, if the leaders reverse G-d’s wrath by prosecuting them.