Castles were very important throughout the Middle Ages for several reasons. You will find plenty of information in the following articles to understand why, how and when castles were built.
Defending a Castle - How were castles defended? Who defended them? What was their objective? Find answers in this article.
Assaulting Medieval Castles - How were castles attacked? This article explains how long it took to besiege a castle and the different techniques used for that purpose.