In this chapter, the Israelites exact revenge upon the Midianites.
Just like that, the digression into Levitical law is over and we abruptly return to the story. Balaam's prophecy back in Num 24 told us that the Israelites would "crush" the Moabites "in days to come". Since this prophecy also predicted the rising scepter of Israel (i.e. some emerging king or power), it is suggestive that this prophecy is speaking of the distant future. However, here we can see that the Midianites (who have been closely associated with the Moabites) are going to be destroyed right now.
Just to give everyone an idea of how weak the Midianites are, the Israelites only send 12,000 men out of their total force of 600,000 and these twelve divisions manage to kill five kings of Midian and take 32,000 female prisoners after killing all the men (young and old) and all the older women. So they probably conquered a population of at least 100,000, probably between 100K and 200K.
Still, with Israel only sending about 2% of their overall force and devastating Midian, that gives us some idea of why Balak felt he had to resort to witchcraft. Ironically, the Midianites are only being attacked now as revenge for their attempted witchcraft and more significantly, "deceiving" the Israelites in the "matter of Peor". We also discover that Balaam was responsible for guiding the Moabites into this treachery, and the Israelites kill him in retaliation. This is interesting because if you read Num 25, there is no mention of Balaam at all and from having read Num 22-24, I would have thought that Balaam wasn't such a bad guy. I mean sure, the LORD rebukes him when he first started off for Moab, even threatened to kill him a few times, but by the time we reach the three blessings and the prophecy, Balaam seems to get along with the LORD just fine.
Towards the end, Balak refused to pay Balaam because Balaam didn't do what Balak wanted. It appears that after the prophecy, Balaam tried to earn his pay by counseling the Moabites to lead the Israelites into idolatry. We have no indication whether Balaam was paid any money, but here we can see the Israelite retribution for Balaam's part in all this.
This is a tough chapter to write about because as we can see, the Israelites end up killing lots of women and children under the direction of the LORD and Moses. I alluded to this when I discussed progressive revelation back in Ex 6. In that case I was referring to the book of Joshua, but this is along the same lines. It is a genocidal war that is fought for a couple reasons. Unlike Joshua, Midian is not within the boundaries of the promised land, so there is no inherent conflict between the two; only a conflict borne out of Midian's fear of the Israelites.
However, the Midianites tried to lead the Israelites into sin. Since the women were primarily involved with this (by inviting the Israelites into pagan orgies, essentially), that is why Moses specifically commands for the women to be killed. The men (and boys) are killed to prevent the nation from ever regrowing and becoming a new threat to Israel. This is a total war, and in a total war everyone is a combatant. The Moabites and Midianites had already shown that they were hostile to Israel, so fighting back is their best option.
More significantly, the LORD is commanding that the Midianites be slain because of their sin. So in that respect, this is similar to the punishment of Sodom and Gomorrah except that vengeance of being wrought through the Israelites rather than through divine intervention of the angels. The children may not be responsible for the sin, but just like the children of Sodom and the children of Dathan and Abiram, they die because of the sins of their fathers. It's not fair, but it's how the world works, both then and now.
Perhaps the bigger question is why the virgin women were allowed to live. There are two reasons. First, they were not involved with the sin of Peor, so they are disassociated from that sin. Second, because a women becomes part of her husbands family upon marriage, the women can be "integrated" into Israel, after that fashion. The men cannot, because the men of Midian will always remain in their "own household" apart from Israel, and will therefore always be a threat.
I feel like I should say more on this subject, but I don't know what else to say. A lot of people tend to have an emotional response along the lines of "what the Israelites did was wrong", and by extension the LORD was commanding them to do something morally wrong (and ergo, the LORD himself commits a moral wrong). This is a very difficult accusation to defend since it borders so closely with the truth. At the end of the day, I think the most meaningful thing I can say is that the LORD has the authority to punish sin, and that is what he is doing here, just like Sodom.
Other than that, we can also see that the spoils of war are divided evenly between the 12,000 men who went out and the congregation who stayed behind. This means that the men who went out would receive far more per capita, but the people who remained behind also profit from their victory. This is because the men who went out to fight did so as representatives of the whole nation, so it's only fair that the whole nation benefit, but at the same time the men who went out took much more personal risk, so they are rewarded for it.
We also see that a small portion of the goods are given to the priests and Levites respectively, like a small tithe (but far less than 10%). The soldiers are also expected to remain outside the camp and purify themselves, which is a requirement if a person goes near a dead body from Num 19:11. Num 19 also contains the requirement that the men wash themselves with the water for impurity.
Lastly, the officers make a donation to the tabernacle because not a single Israelite died during the battle. This is remarkable when you consider the thousands of men they killed, but it's not entirely outlandish. Remember that Simeon and Levi killed an entire town of people (the Shechemites). In that case, it was an ambush and the men were not in a physical condition to fight back, so realistically anyone proficient with a sword could have done the same. As another point, one can also consider the battle of Thermopylae, which was recently made into a movie, where 300-1000 men held off a Persian army of well over 100,000 for several days. Alexander the Great made himself famous through the ingenious deployment of phalanx units, defeating vastly larger armies time after time. If you think about it physically, an army of 100,000 men takes up an incredible amount of space, so only a tiny number of men are going to be on the front line in battle at any given moment of time. This creates opportunities for smaller units to defeat larger armies by massing local force superiority even if outnumbered in the larger battlefield.
While we are not given any details for this battle, it is possible that something similar happened here. In particular, when a panic breaks out in a larger army, it can quickly turn into a rout which destroys any possibility of fighting back. As it turns out, one of the biggest advantages of having a larger army is that it demoralizes your opponent, increasing the likelihood they will retreat and thereby be defeated. I will give more examples when we read through more biblical battles.