How were castles in the Middle Ages protected?

With moats,bridges, big walls, and many guards