Who was affected by the plague in Numbers 25? "While Israel lived in Shittim, the people began to whore with the daughters of Moab. 2 These invited the people to the sacrifices of their gods, and the people ate and bowed down to their gods. 3 So Israel yoked himself to Baal of Peor. And the anger of the Lord was kindled against Israel. 4 And the Lord said to Moses, “Take all the chiefs of the people and hang[a] them in the sun before the Lord, that the fierce anger of the Lord may turn away from Israel.” 5 And Moses said to the judges of Israel, “Each of you kill those of his men who have yoked themselves to Baal of Peor.” 6And behold, one of the people of Israel came and brought a Midianite woman to his family, in the sight of Moses and in the sight of the whole congregation of the people of Israel, while they were weeping in the entrance of the tent of meeting. 7 When Phinehas the son of Eleazar, son of Aaron the priest, saw it, he rose and left the congregation and took a spear in his hand 8 and went after the man of Israel into the chamber and pierced both of them, the man of Israel and the woman through her belly. Thus the plague on the people of Israel was stopped. 9 Nevertheless, those who died by the plague were twenty-four thousand."- Numbers 25:1-9


Did the victims of this plague refer to the execution of those who participated in the Baal-Peor incident? Or did it refer to Israelite bystanders who simply paid a major price for their sinful leaders?
I can only express my own opinion as the account is not specific regarding the question.

It is stated that evil was done (verses 1-3). It is stated what the Lord said to Moses about the matter (verse 4) and what was to be done to the heads of the people (the translation quoted in the OP states 'chiefs' but other translations infer it was the actual heads of the offending persons).

It is stated what Moses said to the judges (verse 5).

Despite all this, still, one man at least paid no heed. And there is afterwards a mention of a plague. I would suggest the plague began when there was lack of absolute and unanimous response to the Lord's words and to Moses' words.

And I would suggest that it was Phinehas' righteous action that stopped the plague, as it is written. I assume that his action - so public and against such a high-ranking Israelite - was the pivot point for the whole congregation of Israel to turn from the evil.

My reading of the passage is that the plague began because of a less than unanimous repentance. And that the action of Phinehas (and the effects of that action on the whole congregation) brought the plague to a halt.

This is a very important passage with regard to justification by faith and is mentioned again in Psalm 106, in even more emphatic terms.