Castles gave the ability to defend a position with a small number of men against a much larger siege force. Once complete, the castle could hold out months or years against a surrounding force. The best castles were designed to frustrate the siege tactics of the time, setting up kill zones that would allow a few archers to decimate attackers and bottlenecking melee forces so they could not use greater numbers effectively.
Castle would normally stockpile supplies so that they would have food, water, arrows, etc even if new supplies were not being allowed in. The larger force performing the siege would also be vulnerable and in the open where they might be vulnerable to a relief force that might be coming to relieve the castle.
Siege forces were not only stuck in the open, they required resupply whose supply trains could be harassed or attacked especially since the siege force would be pissing off the locals while foraging.
The castle also protected its occupants from weather, and in some climates, performing a siege during a northern winter or a desert summer might not be sustainable and could lead to illness or death for many of the attackers.
Castles could also have an offensive purpose, especially when they prepared and stockpiled supplies for an allied offensive force from the outside who could resupply safely at the castle before carrying on to press their attack on a further battlefield.
Once effective cannons were developed, castles had to change design, but against the weapons and tactics of the 10th to 14th centuries, a well designed stone castle was an amazing force multiplier.