There are 2 ways to defend
Hiding your troops is one of the most important aspects and making sure that farms always have enough troops home to start a battle.
Always check the battle log for incoming attacks
Other than crown, alliance castle and flags the defender can take no losses as long as they remain under hospital capacity. As soon as your hospital is full you will begin to take losses.
Early on in the server most of your defending will most likely be able to be done with timing to land just behind the attacker.
Do not get to your friends castle before the attacker because if the embassy is full your troops return home. However if there is an active battle… your troops will join this battle, even if the numbers far exceed what the defenders embassy can hold.
Defensive marches should always include 40% infantry, the longer the battle is going the more people that can join meaning more loss for the attacker.
Always use your highest tier available
Always use 8 troop types (odd/even)
To join a defence with speed ups you simply spam the 50% speed ups until you arrive in the battle. A great way to protect farms and the only way to defend SOS.
Some servers will also speed attack for crown which requires a speed defence.