After the collapse of the Roman Empire in Europe, there was a lot of trouble with pirates and bandits and invasions. Rich people began to build their villas more like forts, for safety, or they moved into old Roman forts. They called these forts castles.
At first, most castles had just one central tower, called the keep. A keep usually had two or three floors, with one big room on each floor. There was one big chimney running up the side, so each room had one giant fireplace to heat it. The castle of the kings of France at Vincennes is a good example of this kind of castle. Other examples are the Tower of London, or the Conciergerie in Paris.
To make it easier to defend these castles, they had only very small slits for windows, and very thick walls. Around the keep there was often a deep, wide moat full of water, and you could only cross the moat by going over a drawbridge. To see a good example of an early castle with a moat, check out William the Conqueror's castle at Caen.
Later on, the countryside got safer. The kings were stronger and could control the bandits better. So rich people began to be less worried about safety and more interested in comfort in their houses. They started to add on rooms around the keep, and to make bigger windows with glass in them, and they put permanent stone bridges over the moats instead of drawbridges. Both Carcassonne and the Tower of London have later additions like this. Then in the 1200s, people in Europe learned about gunpowder from China, and by the 1400s they built cannons that could blow up even thick castle walls, so castles became pointless. By the end of the Middle Ages, people weren't really living in castles anymore.