How exactly are you losing the city?
If it is due to a revolution then stop having revolutions. Do everything you can to prevent them. For example, the city in your screen shot is unhappy. Unhappiness often leads to rebellion. So don't let your city's population grow over the happy cap. That extra population is useless anyway as it does nothing for you, but does cost you a little money in maintenance and also increases instability. Build every building that increases stability. It doesn't even matter if they cost a lot in maintenance - you are making plenty of money to pay for them; on the other hand, there are not very many of them. Don't build any, or destroy them yourself if you already have some, that cause instability - most of the buildings that produce crime and allow you to build some criminal type unit also give instability so get rid of them, at least in your major cities. Reduce your crime rate if it is over 250 since most of the crime buildings that show up at 250 or higher directly cause instability as one of their effects. Pick civics that have better stability bonuses for your situation. If the city is getting close to rebelling, bribe them if possible (this is particularly good if you are not far from researching a tech that will help with the situation). Station more units in the city since a larger garrison (up to some point, anyway) gives more stability. And so on. Check the revolutions screen for any specific issues for that city and the national stability too.
If it wasn't a rebellion, then I guess you just need to defend your city better. You can afford to maintain more units.