It's the death penalty for prophets and dreamers who try to get you to worship other gods.
If anyone in your family tries to entice you worship other gods, you get to throw the first pitch at the stoning. This sounds really cruel, but the writers are trying to make their point.
What about the whole "thou shalt not kill" thing from the Ten Commandment?
Moses talks about scoundrels leading a whole town to worship another god. "Scoundrels" are called "children of Belial" in Hebrew. The phrase literally means "worthless individuals." If the scoundrels infect an entire town with idol worship, the people and the livestock are to be killed, and the town is to be burned. Everything there would then be "devoted to destruction" (17), which means that the Israelites are banned from taking anything from it (12-18).