There are multiple steps that can be taken to defend your city.
Wall defenses will defend your city even if your troops are set to hide. They also have the advantage of not requiring any upkeep. Mines and Artillery will defend against ground units while Stinger Missiles and AA guns will defend against flying units. Combined with troops, wall defenses can easily turn the tide of the battle and allow a smaller force to defend against a larger attacking force.
There will be times when a much higher power player will attack your city. The best way to limit the impact of such an attack is to level your supply bunker. This will defend a portion of your resources and reduce the amount of resources the attacking player would have received. The less resources an attacker receives the lower the chance they will attack you again. This also allows you to retain some resources so that you can continue to grow and expand your cities.
Hiding your Troops / Setting Troops to Defend:
You can choose whether your troops will defend against an incoming attack by selecting the Defend option in your Headquarters. If you believe you can defeat the incoming attack, you can set your troops to defend in an attempt to kill off the incoming attack. The troops you have stationed in the city will then defend against the incoming attack and engage the attacking troops. Defending troops receive a defensive bonus based on the level of Perimeter of the city. Wall defenses will also assist your troops in defending the city.
If you believe your troops will be overwhelmed by the attacking force, it is better to set them to hide. This will prevent the attacking force from killing off your army. However, any resources that are not protected by your Supply Bunker can be looted by the attacking troops. Often it is better to lose some resources than to lose your entire army.
Your alliance members can help defend your city by sending reinforcements to the city that is being attacked. For this to work properly, you must communicate your need for reinforcements to your alliance members and the reinforcements must arrive before the incoming attack. Logisitics Centers will allow a players troops to move to allied cities much faster and can help reinforcements arrive before an attacking army. Reinforcements are a key ability to defend against higher Power players and can allow alliances to defend against must larger players.