From the moment when ancient civilizations started collecting large amounts of wealth, food, resources and power, need for creation of fortified structures arose. In some 5 to 7 thousand years ago, ancient civilizations of India, China and Egypt started making fortified settlements. As centuries and millenniums went on, knowledge of this structures travelled across civilized world, slowly reaching southern, central and eventually northern Europe where people started making hill forts in the Iron Age (1300-700 BC). Roman Empire popularized the use of stonework and defensive construction across Europe, which all culminated in the 9th and 10 century when divided European nobles in France and Germany embraced the custom of making their private castles to protect their wealth, house their troops and rule the surrounding lands. Return of soldiers from the Crusades introduced to Europe advanced construction styles of Middle East, which enabled them to truly start making stone castles of incredible sizes, complexity and defensive capabilities.
Around 4000 years BC, ancient Asian, Middle Eastern and North African civilizations started fortifying their smaller settlements with natural walls (rocks, cliffs), stones and wooden walls. Around 2nd century BC, European dwellers started creating Opidas, hill forts that we heavily used in the defense against advancing roman armies that started conquering France, Germany and England. However, Romans capability to create durable stone and wooden forts and the access to advanced siege weapons proved to be much more effective than simple Oppida hill forts.
Tradition of building castles was not common in Europe during and after the end of the Roman Empire. Their first mass appearance happened in 9th and 10th century AD after the fall of Carolingian Empire, which left large amount of military nobility responsible for the lands of France and Germany. Wanting to secure their rule, every of those lords started building their own wooden defensive structures, which were used not only for military purposes, but also as the centers of their local government, economy and justice. They were usually created from wood, placed on top of a manually created motte, sometimes with a circular ditches, rampants, banks, and stone walls. Central structure (personal residence of the lord, sometimes with public rooms for government work) was often made from stone, but was usually not secured against fire because of wooden doors, roof and windows. During 9th and 10th centuries, creation of forts and castles was usually not controlled by any government (anyone could make one and claim their land, or use it to protect their land against invaders), which led to the creation of tens of thousands castles in the central Europe (4000 in Switzerland alone).
After 1000 AD, castle construction started spreading across Europe with ever rising speed. The biggest rise happened in Italy, with a brief spike happening in Spain after 1020 after skirmishes of Spanish against Muslims from Africa. Majority of the castles created in that time was constructed from timber [Spanish used mud-bricks], with some of them managing to remain standing and fully functional for 4-5 centuries. As 11th and 12th century plunged Europe in almost constant warfare between smaller lords, and national fights, castles made from stone emerged everywhere. Viking war parties forced England and Denmark to start build their first stone castles around 1066, mostly as a coastal defenses. As crafting technology evolved, 12th century castles in England, Scotland and Ireland started taking form of Donjons, large castles with functions that shifted from functional to decorative. However at the end of 12th century, number of new castles dropped significantly, mostly because expenses of stonework. Basic shape of castles was rectangular, with large walls that housed towers that had arrow slits on several levels.
Three Crusade wars significantly changed the way Europeans viewed castles and fortifications. Advanced techniques they encountered in Middle East were soon transferred to France, Germany, Italy, Spain and England, and castles built in Crusader states become templates for many castles built in Europe. Majority of 13th century European castles were created by Military Orders of Knights Hospitaller, Knights Templar, and Teutonic Knights. These castles featured several rings of heavily fortified walls, towers for defense, and wooden or rope bridges between towers and ringed walls that could be destroyed to slow down the enemy and force him to fight against inner walls. These castles also featured machicolations (holes for dropping objects on the ground). The most influential countries for castle innovation were England, France and Spain.
1320s was the decade when gunpowder artillery arrived in Europe, and this caused construction of new wave of fortified defenses that allowed mounting of cannons and firing behind the walls trough vertical slits. Defensive artillery castles sprung all across the Europe [and later New World], but the rise of destructive firepower cannons could create forced castle makers to concede that 100% defense was not possible. Bu middle of 15th century, heavy guns were the preferred tools for attacking castles all across the Europe. The only viable way of defense were much thicker walls, and curved sides (for deflecting shots). Sadly, none of these techniques were easily adaptable to the castles created before age of artillery. In 1500 Italian builders and technicians created angled bastion, which allowed bastions to have heavy contingent of guns that can operate with wide range of targeting. Some castles used bastions so much, that their walls were shaped in the form of star (so called “Star Forts”).
By the end of 16th century, Europe was home to between 75 and 100 thousand castles, some functional, some destroyed. Across the Atlantic, colonial powers built several defensive castles, forts, citadels and fortified manor houses, mostly Spain and France. Their design and build quality mimicked those created in Europe.
Arrival of the 17th century brought sharp decline in the creation of new castles. However, old ones remained in use constantly, some remained in the ownership of the aristocratic families, some were held by military, and many were converted to be centers of local administration, government or court. As open conflict between lords and countries started to subdue, fashion changes caused many nobles to move out from their castles and settle in large and expensive country houses. Popularity of castles returned in 19th and early 20th century, when several of them were created for nostalgic purposes and admiration to the medieval time of chivalry, gothic architecture and renaissance. One of the most famous castles of that time was Castle Neuschwanstein that was built by Ludwig II of Bavaria in 1892. This castle served as an inspiration to all modern times depictions of castles, most famously Disney’s Sleeping Beauty's Castle.