Commanders can appoint specific Heroes to defend the Base in case of an enemy attack. This is done through the Defenses menu of the City Wall.
There are two positions for City Defenders - one for a Primary Defender and one for a Secondary Defender. To assign a Hero as a Defender, you can tap on any of the two positions to open the Hero roster. In the roster, all hero skills can be reviewed, and tapping on a Hero's portrait will assign them to the chosen Defender position.
Any Hero can be freely assigned to or unassigned from any of the City Defender positions.
Being appointed as a City Defender doesn't prevent a Hero from being sent out on marches or from being assigned to a Command Station.
Defending the Base
When your Base comes under enemy attack, only one Hero can lead your armies in the defensive battle. Heroes, who are out of the Base at the time of attack, can't participate in the defense, even if they return while the battle is still ongoing.
If the Hero, assigned as a Primary Defender is present in the Base at the time of attack, they'll be the one to lead the armies in battle. If the Primary Defender is not present, then the Secondary Defender will step in to defend the city.
If neither of the two heroes, assigned as City Defenders, are present at the time of attack, then the default Defender, which is L.I.L.L.Y., will participate in the battle.
Note: L.I.L.L.Y. is an exception, because she will participate in a city defense even if she's currently out on a march.