TLDR: Castles were fortified structures built during the Middle Ages by nobility and military orders. They were private residences for lords and nobles, but also served as centers of administration and symbols of power. Castles originated in Europe in the 9th and 10th centuries and took on various forms and features. They were both defensive and offensive structures, providing protection from enemies and serving as bases for raids. Castles had features like curtain walls, arrowslits, and portcullises, and often had a central keep and surrounding baileys. Over time, castles lost their military significance and became more focused on domestic and symbolic purposes. They were eventually replaced by artillery forts and country houses. The word "castle" comes from the Latin word "castellum," meaning "fortified place."
Castles were built in response to the need for defense and protection during a time of insecurity. They were often constructed on mottes, which were artificial mounds with a flat top, and surrounded by moats. The entrance to a castle was usually through a gatehouse, which had various defenses like portcullises and arrowslits. The main defensive walls of a castle were called curtain walls, which were thick and tall to withstand attacks. Battlements, such as crenellations and machicolations, were added to the walls for further protection. Inside the castle, there was usually a great hall where the lord received guests and held events. Castles also had features like posterns (secondary gates), keeps (strong towers), and moats (ditches filled with water or dry).
The origins of castles can be traced back to earlier forms of fortifications, such as hill forts and oppida. The rise of castles in Europe was influenced by the need for defense against attacks by Magyars, Muslims, and Vikings. Castles also served as centers of administration and control over land. The construction of castles required permission from the king or other high authority in some cases. Castles varied in design and features depending on the region and time period. They played a significant role in medieval society but eventually lost their military significance and were replaced by other types of fortifications and residences.