Even in a defensive war (which will be the presumed context going forward), first you offer peace. Then you offer anyone from the enemy who wishes to flee to be able to flee, and let them - both of these are for the sake of being compassionate and humanitarian (see Sefer HaChinuch 527)*.
We, who have learned Torah for 3000 years and have absorbed its unique lessons in the infinite value of human life, do indeed have a deep seated fear that we will end up killing someone innocent (see Tanchuma Lech Lecha 19), which should lead us to be a compassionate nation who tries to minimise the death of the enemy population in any way we can, so long as those are not ways that will increase the risk or sacrifice to our own population in any way. Some say that we are bound by international law when it comes to wars in galus, so note that this is indeed something that would go above and beyond that.
However, once all of the above is done and the enemy has not surrendered or fled, you go to war, and you do so in order to win, in a way that brings back peace, which is a decisive win that totally subjugates the enemy to put them off trying again. This is the only moral way to fight a defensive war, as anything less than this will lead to escalating violence and entire generations growing up knowing nothing but war, leading to far more destroyed lives and death than a single, decisive victory.
The only thing that may be relevant is what to do with the enemy women and children.
Note, this halacha about sparing the women and children (Rambam Melachim 6:4) is presented in the same halacha the one that says one should kill every adult male. Note how the halachot do not make a distinction between "combatant" and "civilian". It makes 3 distinctions:
- Those who wish to flee.
- Men.
- Women and children.
It is pretty clear from the context then, that the Rambam is not saying that one should sacrifice one's own in order to protect the enemy, even the women and children. One should keep the women and children alive lechatchila, when there is no reason (quick and lasting peace, or protecting one's own) to kill them. A defensive war by definition is there to defend one's own nation! (and therefore is not to do with viewing one's own side as inherently more valuable). An even more compelling reason to accept this (logical) argument is by looking at the pasukim in Devarim 20:13-14 which reveal that we are talking about a situation where ונתנה ה' אלקיך בידך, Hashem gives them over to your hand, i.e. we have already won and are dealing with the survivors.
Everyone knows this, not a single nation in the world will command their soldiers to lay down their arms and let them or their civilians get slaughtered by an enemy combatant because he is hiding behind children, nor would they expect the soldiers to retreat and allow the enemy escape to fight another day.
Torah never makes a distinction between soldier and civilian because doing so is evil: every human being is infinitely valuable, and we never make any human beings "expendable", which is what this false dichotomy of "soldier" and "civilian" implies - the soldiers are somehow fair game and expendable ch'v. It is nation against nation in a war, not army against army, and the law is one fights in order to win and bring peace, as has been said. The calculations of who to kill are centered around this principle, rather than comparing the value of human lives, therefore one does this in whatever way will be the quickest, and involve the least bloodshed to both sides, but with veto priority to one's own side.
Who makes this calculation? The halacha is very clear: the military commanders, and not the political leaders (and kal vechomer not the civilians, no matter how opinionated and passionate). [source to follow bli neder, I must go get ready for Shabbat now]
tl;dr The basic rule is this: the goal of the defensive war is to eliminate the threat to peace. Therefore the victory must be swift and decisive, and not a single person more or less than necessarily is allowed to be killed in order to achieve that goal. There's no distinction between combatants and non-combatants in terms of human value, so the only real consideration is what will bring the most effective peace, at the minimial cost to one's own side first, and the other side second.
* This is debatable, according to Rav Shlomo Goren from his letters in 1982, the Ramban and the Sefer HaChinuch imply that a defensive war is similar in this regard to a "permitted" war, rather than an "obligatory" war.