There are three advantages.
The first is that you arguably have cover for your units. The cover is absolutely abysmal, and some say that it would be better to fight in the open, but if you use them, the walls have high hit points. I guess that is good.
The second is towers. You get five towers with a 9 range. They hit with strength 7-13. Something like that. They are OK.
Third, you have city operations. These apply to combat anywhere in the city's borders, and is where the defender's advantage is really felt. Some of these operations give exceptional power to your units, or debuffs to your opponents. If you know you are going to be attacked, stack these up.
Know though that no matter what you do, the garrisons are un-modded, and do NOT have any city advantages (well built, etc...). So they SUCK. You will lose badly to any army if you do not have additional units to bolster their numbers. Once in a while though, the computer does not attack with overwhelming strength, and actually loses. It is rare, but sometimes you can defend against a computer. Don't rely on that though.