Below is a list of topics related to defense. Some of it overlaps with the topic on Village Management, so consider taking a look at that, too.
Ever have the experience in a Raid where your Mortar fires at a group of hostiles, who break through your Wall, and the mortar then falls harmlessly behind them? This might indeed be because your enemy is too strong for your walls. But it might also be because your walls are too weak for your opponent. If that sounds to you like a meaningless distinction, it really isn't.
Walls are the understated aspect of a good defense. It's tempting to focus your energy on upgrading your defensive weapons, but walls can be used to enhance their strength. Point is, walls aren't a total waste of resources. What's the use of a high-level Archer Tower if your opponent's giants can drive right through your low-level walls and grind it into powder? Would you rather have a mid-level Wizard Tower surrounded by thick walls, or would you take a high-level tower protected by not much at all?
Walls can also be used individually to decrease the space available for your enemies to spawn troops.
Placing defensive buildings
Remember to keep in mind that outside of your village area is effectively a dead zone, where no enemies can stand and none of your defenses will fire. That being the case, any part of a Defensive Building's firing range that extends into this dead zone will be wasted. That being the case, it's difficult to set up your defense without wasting firing range, so consider it carefully. The main thing is to spread your defensive range as evenly as you can over the areas you want to protect the most.
Walls are no impediment to your enemy's flying troops, and Air Defenses are no threat at all to your enemy's ground troops. It's a good idea, generally, to place your air defenses closer to the center so that there is as much air coverage as possible, with the least amount of range overlap as possible. Your Air Defenses are the type with the most firepower of all defenses against flying troops, but they are also the most conspicuous of all air defensive buildings, so an opponent who means to use a wave of Dragons, Balloons, etc. against you will target your Air Defenses very early on in the fight.
The Mortar has a range of 4 to 11 tiles, which makes it the widest ranging defensive building. Note the key vulnerability of the Mortar -- that if an enemy can get within four tiles of it, this defense is defenseless. It has a vulnerable range within four tiles of it, so if the enemy gets within that range, the Mortar will soon be rendered useless, no matter how powerful it is. For these reasons, like the Air Defense, many experienced players find it useful to place the Mortar closer to the center of the village.
Traps are where it starts getting dirty, and they can be used to great effect. The goal is to place them strategically, so that they are in places where your enemies' troops are most likely stumble into. Create small passageways to place Traps in since most ground troops will go whichever direction will get them there fastest, often avoiding wall-breaking. Being the idiots they are, they walk right into your Traps.
Try not to place them haphazardly. It's easy to make traps an afterthought, placing them after the visible defenses have been placed. It can be difficult, but consider incorporating Traps into your defense plan.
Air Bombs and Air Mines
Basically, these are coffins packed with skeleton troops, and the concept is very simple: your enemies come within five tiles of one of them, and a swarm of these diabolical little bastards comes pouring out. They aren't particularly deadly until they have been upgraded a few times, but they're enough to cause trouble and distraction and take off a few hit points from your enemy, making it easier to finish them off as they get closer to your defensive buildings.
There isn't much that is more gratifying than seeing a crowd of your enemy's ugly Giants go flying out of the picture after stepping on a Spring Trap, and there is little more distressing than seeing your own crowd of noble Giants flying out of the picture in an attack on another player. This being the case, the optimal place for a Spring Trap is a small space where you are sure that your enemy's troops will bunch together.
Upgrading defensive buildings
Unless you are battling constantly, you will generally make the most steady progress by upgrading the most affordable buildings first. "The rising tide lifts all ships," is the idea. It will take you longer to save up your gold to purchase more expensive (and yes, more powerful) defenses because other players will always come along and steal some of it. They can't steal money that's already been spent, however, so sinking a fistful of coins into upgrading a bomb now is better than saving up for an Archer Tower. By the time you save up the gold, the equivalent price of it may have been stolen from you at least once, and you could have upgraded two bombs in the meantime. You've effectively had to pay for it twice.