The defensive army of a player's town is an army of maximum eight units that defends the town in case of an attack from a neighboring player.
Neighbors may attack for plundering, battle points, to get on the champion leader board to win medals, or to keep the game interesting/boredom.
Selecting your army
To set a defensive army in your city, go to the army management screen, click on the blue shield on the left and then choose any unit you have, and it will turn blue, meaning it is defending your city. If you want to deselect a defending unit, make sure you've chosen the blue shield and click on a chosen unit that is shown with a blue health bar and their unit picture will turn green again. Choose a good mix of troops (up to eight units). They don't have to be clicked in to the top "selected army" screen for you to choose them, they just need to have been chosen and appear to have the blue health bar then you know these are your defending units. If you don't choose anything you automatically end up with 2 spearfighters which is an extremely hilarious and weak defense, although it can be a deterrent for those trying to attack you for points. Defending troops can still fight in offensive battles, but if they are lost they will no longer defend your city.
After you've been attacked by a neighbor, your defensive troops are immediately fully healed after the attack (to whatever life they had when they were chosen as defense troops). This means that everyone who attacks you will face units with the health they have when they were put into defense, plus the health they recover if they are damaged when you put them onto defense. In other words, any damage an attacker does to your defending units is purely for that player, and does not apply to other players.
Units heal automatically through time. So if you chose spearfighters for instance, with only 1 health bar left, if someone attacks your city before the time to fully heal has passed, they'll be fighting a weakened unit with whatever health that troop had.
If you use your defense troops for attacking and they get hurt, you could be increasing the attackers chances of winning because that is the strength they will have to defend with during a defensive battle.
Weakened units do less damage in both attack and defense so its a good idea to have decent health remaining when you chose a defensive army.