…you shall hang him on a gallows. (Deuteronomy 21:22)
If someone is convicted of blasphemy or idolatry, the penalty is death by stoning. A person sentenced to such an execution was hanged on the gallows for a short time after his sentence was carried out.
The reason for this mitzvah is to serve as a deterrent. The people should see the consequences of blasphemy and idolatry so that they’ll know to keep far away from these things. Nevertheless, even the executed blasphemer is entitled to a measure of dignity, as we will see in the next mitzvah.
The convicted person was not hanged by the neck; he was hanged by his hands, which were tied together.
This mitzvah only applies in Temple times, when capital cases are judged. It is discussed in the Talmud in tractate Sanhedrin on pages 45b-46b. It is codified in the Mishneh Torah in the fifteenth chapter of Hilchos Sanhedrin and is #230 of the 248 positive mitzvos in the Rambam’s Sefer HaMitzvos.