I don’t know if this will be a good answer, because I’m not too knowledgeable about this stuff. Be sure to look elsewhere to find more definitive info.
First of all, the commandment translates literally as “do not murder”. God did not prohibit the Israelites from waging war. He made it clear that war wasn’t all that great, and that if everything were as it should be, there’d be no war. But God never said anything to forbid self-defense or war, though He did say that peace was preferable and that turning the other cheek was good.
Second, you have to understand the context. God was basically using the Israelites to smite these peoples, as a punishment for their disobedience. (And for not giving land to Israel when directed, if I recall correctly.) A lot of them had shadowy traditions of monotheism pretty much like Israel’s (not surprising in related peoples), but what they practiced was the worship of many gods – some of them very cruel gods, like Moloch. So there was smiting. As when He sent fire on Sodom and Gomorrah, or when He permits storms and earthquakes and disasters, God didn’t spare children or women.
But God always controls how all of us die, and we all die sometime. There aren’t many worse ways to die than in an ancient war; but it may well be that many of the innocent victims went straight to Heaven, and may be happier than we are. It’s a hard question.
Third, ancient war was usually about taking slaves, for work or sex, and taking spoils of gold and other goods. The only reason Israelites were sparing anyone was to get money from them or make them slaves. God did not want the Israelites to go to war for money or slaves. So He punished them if they took spoils or slaves for themselves; and anybody who hadn’t fled was killed.
Fourth, God did not establish all this killing in warfare as a general rule. In wars afterward, you see Israel being directed to conduct a fairly humane kind of warfare, as warfare in the ancient Middle East went. Christians received even more humane commands. But when He was setting up the Israelites in their land, He commanded much more sternly, in order to make sure that they became and stayed their own people.
It’s pretty rough stuff, I agree. I wouldn’t like to be smote by God in any way. But it’s not just random commands of limitless violence at all times and all places.