Deu 20:16 But in the cities of these peoples that the LORD your God is giving you for an inheritance, you shall save alive nothing that breathes,
Deu 20:17 but you shall devote them to complete destruction, the Hittites and the Amorites, the Canaanites and the Perizzites, the Hivites and the Jebusites, as the LORD your God has commanded,
The Deuteronomy passage gives a reason for the genocide:
Deu 20:18 that they may not teach you to do according to all their abominable practices that they have done for their gods, and so you sin against the LORD your God.
A few notes: God wants the genocide because He fears that Israel will adopt their practices (a prediction which comes true). This command only applies to inhabitants of the Promised Land. Foreign peoples were only to have the fighting age males killed, and this is only if they are in war with Israel and only if they refused to become tributaries (Deu 20:10). An underlying justification for the genocide is that the inhabitants are intolerably wicked. This is explicit in Leviticus 18:
Lev 18:24 “Do not make yourselves unclean by any of these things, for by all these the nations I am driving out before you have become unclean,
Lev 18:25 and the land became unclean, so that I punished its iniquity, and the land vomited out its inhabitants.
Lev 18:26 But you shall keep my statutes and my rules and do none of these abominations, either the native or the stranger who sojourns among you
Lev 18:27 (for the people of the land, who were before you, did all of these abominations, so that the land became unclean),
Lev 18:28 lest the land vomit you out when you make it unclean, as it vomited out the nation that was before you.
Israel is allowed to dispossess the current inhabitants because they are exceedingly wicked. God lists their sins: incest, sex with both a mother and daughter, sex with a menstruating woman, adultery, sacrificing children, homosexuality, and bestiality. Murder and sexual sins are the chief of God’s concerns. Of important note, God is not overly concerned about foreigners worshiping other gods. As Christine Hayes writes:
Moreover, like Kaufmann, Sarna stresses (Genesis, 53) that humankind is not, in this story, punished for violations of religious sins, that is, for idolatry or failure to worship the god of Israel. It is the view of the Torah books that each nation worships its gods in its own way, and only Israel is obligated to worship the god of Israel. The other nations are not held accountable for idolatry in the Torah as Israel will be. But all peoples, Israelites and non-Israelites alike, by virtue of having been created by the one god and in his image— even though they may not know or worship that god— are bound to a basic moral law that precludes murder and all forms of physical and social violence.
What this means, and a major factor in considering the justice of Israel’s genocides, is that the wars of Israel were not Holy Wars. Additionally, there does not seem to be a general command of Israel’s tributaries to worship Yahweh. Israel’s wars were not jihad against the “infidel” (sometimes Israel warred against Israel for moral reasons). The inhabitants would not have been dispossessed of the land if they were righteous. The double coincidence of residing in prime real-estate and being intolerably evil led to their downfall. [There is the curious case of Gibeon, who was able to avoid genocide with a crafty peace treaty. This trumped God’s command for genocide and was honored by Israel.]
Instead of Holy War, God’s commands had a foundation in moral, retributive justice. The pagan nations were involved in heinous sex crimes and in murder of children. It was this that God was punishing. God tells Israel that if they commit the same, God will likewise do to them. Israel could easily be the recipient of God’s punishment.
Deu 9:5 Not because of your righteousness or the uprightness of your heart are you going in to possess their land, but because of the wickedness of these nations the LORD your God is driving them out from before you, and that he may confirm the word that the LORD swore to your fathers, to Abraham, to Isaac, and to Jacob.