The Clan Castle is perhaps the best defensive structures in Clash of Clans, but only if you place it in the center of your base. The way the structure works is that when enemy units enter the Clan Castle’s range (it has a very long range), the units will pour out of the Clan Castle and attack the enemy’s attacking units. Note that your Clan Castle troops are capable of hopping over your own walls.
If you place the Clan Castle outside of your base, the enemy can simply lure away the units inside the Clan Castle and easily kill them out of range of your towers. If you keep the Clan Castle inside your base, the enemy will have to take damage from both your Clan Castle units as well as your defensive structures in order to take out your base.
Generally, it is best to ask for Archers to keep in your Town Hall for defensive purposes. This works very well because Archers will often shoot at melee units from behind your walls, allowing them to shoot down Giants and Barbarians without taking damage. Very high level players may choose different units, such as Dragons or Golems, but this requires a clan where members will donate these troops.
Note that this tip really only applies to players at Town Hall 7 or above. At earlier Town Hall levels, you do not have enough buildings to actually prevent the enemy from luring out units in your Clan Castle. Town Hall 7 units can still be lured, but it is tougher. At Town Hall 8 and up the enemy will have to destroy some buildings or a decent number of units to force troops out of your Clan Castle.