Why did God order the Israelites to kill all the males in a land after capturing it but spare the females?
You might be helped with my answer to the following question: Why did Moses order female virgins to be kept alive but all others killed in Numbers 31:17-18?
In some ways your question is similar to the one I just cited.
First, God commanded Israel to annihilate completely the inhabitants of Palestine/Canaan (specifically the "tribes" and "nations" therein) because of their abominations and defilements. In Genesis 15 we read the following:
"Then in the fourth generation [after Israel's exodus from Egypt] they will return here [to the promised land], for the iniquity of the Amorite is not yet complete" (vs.16).
The word Amorite, as it is used here is a synecdoche in which the Amorite nation (or peoples) stands for all the nations Israel was to dispossess in their conquest of Canaan. Depending on which Old Testament book you consult, the number of nations Israel was ordered to dispossess ranges from four to ten (see, for example, Genesis 15:19-21; Exodus 3:10; 13:5; 23:23; 33:2; Deuteronomy 20:17,18; Joshua 3:10). What they all had in common besides their rampant immorality was their occupation of the land God promised to Abraham hundreds of years before Joshua came on the scene.
In the interim between Abraham and Joshua, the sins of the "Amorite" became worse and worse, to the point where God's patience with them came to an end because of their wickedness. In other words, when nations reach a certain level of wickedness, God is forced to intervene with severe punishment. As the New Testament tells us, God is not willing--nor does in take delight in--the death of anyone, but He desires that all people everywhere repent (see 2 Peter 3:9). When a people is beyond redemption, God's mercy gives way to His justice.
In the chapter from which you quote verses 13 and 14, we also read the following commandments from Jehovah:
"'When you approach a city to fight against it, you shall offer it terms of peace. If it agrees to make peace with you and opens to you, then all the people who are found in it shall become your forced labor and shall serve you. However, if it does not make peace with you, but makes war against you, then you shall besiege it . . . [and] strike all the men in it with the sword'" (vss. 10-13).
Moreover, further down in the chapter we read this:
"'Only in the cities [i.e., nations] of these people that the LORD your God is giving you as an inheritance, you shall not leave alive anything that breathes. But you shall utterly destroy them . . . so that they may not teach you to do according to all their detestable things which they have done for their gods, so that you would sin against the LORD your God'" (vss.16-18).
Clearly, only the nations that were "close by" were marked by God for annihilation; as for the "cities that are very far from" Israel which were "not of the cities of . . . [the] nations nearby," Israel was given permission either to make peace with them and then make them slaves, or if they refused to make peace with Israel, to kill only the men and spare the women and children (see vs.18).
The clear inference, then, is that the nations far away were not as wicked as the nations that were close by. Seeing this, God spared the women and children.
In conclusion, we needn't dwell on the baseness of the sins of the "Amorite." Frankly, the sins are almost too revolting even to enumerate. When any people group becomes so reprobate in their behavior, God resorts to drastic measures to prevent the cancer of their sin from spreading to neighboring nations. A biblical expression for a nation's extreme wickedness is found in Leviticus 18:25 (NIV):
"Even the land was defiled; so I punished it for its sin, and the land vomited out its inhabitants."
What a graphic expression in describing the utter depravity of a nation: its people become so wicked that they sicken the land they inhabit.
Israel, on the other hand, was first to purge the land of this wickedness, and second, to set an example of godliness, righteousness, and justice to the nations surrounding them, so that these nations who would not only fear the "one true God" of Israel, but would be attracted to Israel's God and seek Him out. God was not just Israel's God, but God over all the earth. As 2 Chronicles tells us,
"For the eyes of the LORD move to and fro throughout the earth that He may strongly support those whose heart is completely His" (16:9, my emphasis).
In fact it was an (ancient?) common war usage because males are usually consider more dangerous. Jugurtha, king of Numidia, in 112 BC killed all the adult males of Cirta. Thucydides reports an interesting debate about the fate of the Mytileneans during the Peloponnesian War there were two different position: Kleon wanted to kill all the Mythilenens adult males while Diodotos only those most responsable of the revolt in question. Killing the males means avoid possible rebellions, men are stronger and in all the patriarchal societies are the base of the political and militar affairs. So kill all the males of a comunity means for the most cases destroy the identity of the comunity. While women can be kept as slaves (maybe concubines, but YHWH forbid to have sons from non-ebraic women Deut 7:3) depending on the needs. In fact there are more drastic solutions of killing the male children too, you can find it in Numbers 31:15-18 in these verses is written to kill even non-virgin women this can be hardly explained rationally maybe was supposed married women were less incline to be subdued. The most drastic solution is of course to kill everyone (Deuteronomy 20:16).