Castles were needed in medieval times to provide protection to the lord from foreign invaders. They were also a physical reminder of the lord's power over his lands.
Castles in the medieval time period graduated from hastily built timber castles on mounds of earth with a moat to massive stone fortresses. In this time period, Europe was ruled by feudalist societies. In these societies, the king owned all of the land and was the ultimate ruler of the country. He gave sections of land to his nobles to rule for him in his stead. It was these nobles along with the king and his entourage who lived in medieval castles.