In Civ5, what are the deciding factors for garrison vs. fortify vs. attack?
Solution 1:
You cannot fortify a unit inside a city, you can only garrison it. When the city falls, the garrisoned unit will be destroyed.
I've never found it worthwile to garisson a melee unit, but a ranged unit in a city can be very deadly. If you only have a melee unit in your city, I would use it to attack the enemy, if you think you have a decent chance of defeating the enemy army. The added attack rating for the city for a garrisoned unit isn't worth much in my experience. If the enemy army is overwhelming I would garrison the unit as long as the city has more health than the enemy can take down in one round, and before he captures the city I would disband the unit to get at least some gold back.
Solution 2:
When defending a city I put ranged unit inside it therefore it can have a better reach to the front line without danger of being destroyed however note that garrisoned units will no longer be garrisoned if you use them to attack an enemy.
I put my melee unit(s) close to the city on rough terrain tiles and fortify them. The fort or even a citadel is ideal. The preference for building a fort is rough terrain tile parralel to the front line from the city itself. If you have a spare great general build a citadel (they are great!) in front of the city, meeting your enemy.
I put cavalry or tanks just behind the city and use them for hit and run tactics making sure they can retreat to the same spot after attack.
If the city is likely to fall try to see that in advance before its too late to make an organized retreat. Spare your units (they are worth a lot) - evacuate the city to heal and regroup. Use your rearguard units to clear any flanking enemy for safer and faster withdrawal.