You shall have a place outside the camp… (Deuteronomy 23:13)
When going to war, the army had to designate a place to serve as latrines for the troops. Doing so kept the soldiers from taking care of their bathroom needs just anywhere.
The reason for this is that the army relies upon God’s providence to succeed. As such, they rely upon God dwelling among them, metaphorically speaking. Therefore, the camp should not be filthy and disgusting. It’s a lot easier to strive for a relationship with God in an environment that is clean both physically and spiritually. For this reason, those who were afraid to die in battle because of their sins were excused (see Mitzvos #525-526). This purified the camp spiritually. Designating a latrine area helped to purify it physically.
This mitzvah applies in Temple times. It is discussed in the Midrash in the Sifre. It is codified in the Mishneh Torah in the sixth chapter of Hilchos Melachim. This mitzvah is #192 of the 248 positive mitzvos in the Rambam’s Sefer HaMitzvos.