Defending in Clash of Clans basically comes down to two things, the strength of your defensive structures, and your base layout. Once you unlock the Clan Castle, you are also able to store troops in there who are triggered, once an enemy gets close enough, to help defend your base.
Defensive structures can be things like Walls, to protect buildings and direct enemies, as well as more physical deterrents so as Cannons and Mortars. You are also able to place traps, such as Bombs, which activate when an enemy unit steps on them.
These go hand-in-hand with your base's layout, as you ideally want to channel enemies down a certain path into the best defended area. Once an enemy places their units, there is nothing they can do to change the path that they take. This means that if you learn how the AI works in Clash of Clans, you can predict where enemy units will end up.
There are many different ways to defend a base