In the historical context of the Israelites' journey to the Promised Land, they encountered various peoples and cultures, including the Midianites. This command to treat the Midianites as enemies and kill them comes after the Midianites had led the Israelites into idolatry and immorality, which was a grave sin against God. The Israelites were called to be a holy people, set apart for God, and this required them to avoid influences that would lead them astray from their covenant with Him.
The directive highlights the seriousness of idolatry and the need to protect the community from spiritual corruption. It reflects the broader biblical theme of the struggle between faithfulness to God and the temptations of surrounding cultures. While the specific command was for a particular time and situation, the underlying principle of guarding one's spiritual integrity remains relevant. It encourages believers to be vigilant in maintaining their faith and values, resisting influences that could lead them away from their relationship with God.