The women of Moab and Midian, following the advice of God through his prophet Balaam, invited the Israelite men to have sex with them and worship the gods of Moab. This made God angry, so he decided to kill all the Israelites yet again, and he told Moses, the leader of Israel, to kill all the leaders of Israel. So Moses told the judges of Israel to kill all the Israelites who had worshipped a Moabite god.
God had already killed tens of thousands of Israelites himself, when an Israelite leader brought a Midianite woman into his tent. When Aaron’s grandson Phinehas saw this, he followed them into the tent and killed them with a spear. This somehow turned away God’s anger and convinced him not to kill all the Israelites. God was so pleased with Phinehas that he made a covenant of peace with him and said the descendants of Phinehas would always be God’s priests.
Because the Midianites followed the advice of God’s prophet Balaam and enticed Israel to be unfaithful, God told Moses that the Midianites were enemies and should be killed. But he told Moses to leave the Moabites alone, even though they did the same thing. (Later, the never-changing God changed his mind about that.)
Moses told the Israelites to go and fight the Midianites. So Israel killed all the men of Midian, stole their belongings, and burned down their homes. This made Moses angry. He said they should have also killed the women and boys, and kidnapped all the virgin girls. So they did.
The end.
The moral of the story
Don’t take advice from prophets of God, or your nation will be destroyed.