The word translated 'heads' in the KJV is רֹאשׁ and like most words it has a wide semantic range, it can mean 'head' as in a part of the body, but it can also refer to 'a head' as in a leader or chief (among other things), see for example:
Exodus 6:14 These are the heads of their fathers' houses: The sons
of Reuben, the firstborn of Israel, were Hanoch, Pallu, Hezron, and
Carmi. These are the families of Reuben. (Exo 6:14 NKJ)
Or
Numbers 14:4 So they said to one another, "Let us select a leader and
return to Egypt." (Num 14:4 NKJ)
In both verses it is the same Hebrew word.
More modern English translations reflect this latter meaning in Numbers 25:4
For example:
Numbers 25:4 Then the LORD said to Moses, "Take all the leaders of
the people and hang the offenders before the LORD, out in the sun,
that the fierce anger of the LORD may turn away from Israel." (Num
25:4 NKJ)
Numbers 25:4 And the LORD said to Moses, "Take all the chiefs of
the people and hang them in the sun before the LORD, that the fierce
anger of the LORD may turn away from Israel." (Num 25:4 ESV)
Numbers 25:4 The LORD said to Moses, "Arrest all the leaders of
the people, and hang them up before the LORD in broad daylight, so
that the fierce anger of the LORD may be turned away from Israel."
(Num 25:4 NET)
Numbers 25:4 The LORD said to Moses, "Take all the leaders of
these people, kill them and expose them in broad daylight before the
LORD, so that the LORD's fierce anger may turn away from Israel." (Num
25:4 NIV)
So whilst the mode of execution is not actually explained the reference to 'heads' seems to be understood by most translators as referring to the leaders of the people who sinned. The commentators also seem to agree, for example:
Moses was charged to literally “take [qaḥ] all the leaders of the
people [roʾšê hāʿām] [Cole, R. D. (2000). Numbers (Vol. 3B, p. 438).
Nashville: Broadman & Holman Publishers.]