Yehoshua captured Yericho, but the city was full of sorcery, idol worship, and evil forces. Therefore, Yehoshua wrote the prayer of Aleinu to rid the evil forces, thereby allowing the Divine Presence to rest in the city. We say Aleinu at the end of our prayers because we are about to leave the holy synagogue and go out into the world full of evil forces. This prayer protects us from these evil forces and reminds us to remain focused on holiness.