It is usually necessary to implement an effective anti-air strategy. There are four parts to an effective anti-air strategy: Scouting, construction, intelligence and defense.
The first step in an effective anti-air strategy is understanding the threat. Scouting refers to getting information about what kind of threat the enemy poses, typically by sending an air scout over the enemy's primary and secondary bases. You should look for the tech level of any air factories, as well as any existing air units that may be patrolling or gathering for attack. Although scouting runs like these are often fatal for the scout, scouts are very cheap units to produce and should be used liberally.
Based upon the results of scouting, you should select which anti-air units to build. There are a variety of anti-air structures, fighters, mobile anti-air units, and naval units with anti-air capabilities. Which ones you should build depend on your economy, existing factories and what targets you want to defend, but you should always build more than you think you need. Anti-air units can be grouped roughly into fighters, AA structures and mobile AA.
Fighters, in the form of T1 Interceptors, T2 Fighter/Bombers and Combat Fighters, and T3 Air Superiority Fighters, are often very effective because they can easily fly to where they are needed, but they are also susceptible to anti-air, and they have the drawback of fuel. Fighters can also be easily distracted from important targets like bombers and gunships by enemy fighters. On the other hand, fighters are very effective at killing enemy bombers because they can follow these slow-moving targets and fire long salvos.
Interceptors are good for early on air defense, or for when you need anything in the late game, with a mere 5:00 minutes fuel. T2 Fighter/Bombers can defend against land/sea units as well as air units, the latter more effectively than interceptors, and with 8:20 minutes' fuel. The Aeon combat fighter is basically a Fighter-bomber with no air to ground weapons and a higher speed. The T3 ASFs are hands-down the best airborne unit you can get in this field, being fast, strong, and powerful, with 16:40 minutes of fuel, but are also expensive in price and time, especially compared to F/Bs and CFs. Still, F/Bs can be outrun by strategic bombers, something that ASFs and CFs have no problem with.
Structures generally hit enemy air units very hard, but because air units can easily fly out of range, they are less effective against bombers and spy planes unless they are built in very large numbers. T1 AA guns are good if you need quick defense, but otherwise should be replaced by T2 Flak guns and T3 SAMs, since SAMs can hit T3 fast aircraft and Flak guns do the same job as T1, but much better.
Mobile anti-air units have the advantage of mobility, which allows them to protect a land or sea force, but the disadvantage of vulnerability to other types of attacks. It can be difficult to order mobile AA to attack specific air units because if the targets fly elsewhere, the mobile AA may move away from the group it is intended to defend. T1 AA guns are quite spammable and T2 Flak are just as good as their stationary counteparts, if only weaker in health points and max range.
Intelligence is crucial to an anti-air strategy. Anti-air units have a range that is greater than their vision radius, so they become much more effective when the enemy aircraft are within range of a radar unit.
Every effective air attack will at least do some damage to its target. It only takes a single pass by two or three Tech 3 bombers to destroy an ACU, and it can be difficult to kill them before they release their payload on their initial run, so defensive structures are vital. The only defense that is effective against bombers is shields. Build shield generators around your firebases and vital structures, and consider upgrading your ACU with a personal shield generator (if available). This will blunt an incoming bomber or gunship attack and give your anti-air units time to destroy the attacking aircraft.