There are basically three kinds of castle: those used to defend, those used to control movement, and those used to project power.

So, defensive castles are intended to keep people alive until the attackers are gone. They will be located in hard to access locations with layered defenses. Masada and Machu Pichu come to mind. These have more in common with Tibetan Llamasaries than with European castles.

The walled, turreted castle with a keep so common in Europe existed to exert control over trade, with a secondary function of being along a likely invasion route. The little castle on Loch Ness and almost any other castle in Europe was one of these. They are strongly defended. But their primary purpose was to collect taxes.

William the Conqueror was a big believer in the idea of the projection of power, and so he built castles in enemy territory which forced the enemy to fight on their own ground. These castles could be used as supply depots and as bases for striking forces, but their primary purpose was to be force multipliers. The enemy had to attack them before they could carry the fight to him, and relatively small garissons could wreak havoc on much larger attacking forces. This was also the strategic role of the stockade forts of the American West.

Outside of these roles are the estates, mansions, and palaces of the wealthy. These structures may have had a secondary role as a castle, but their primary role was as a way to impress. Windsor comes to mind.

As another poster suggested, the powerful may have more than one castle. Let us presume a monarch has a royal palace. Its purpose is as an administrative center, with a secondary function as an official residence. Thus its inner keep is isolated from the rest of the palace with controlable access.

The monarch also maintains a summer residence on a sheer mountaintop at the end of a well defended alpine valley.

In addition, the monarch's ancestral home is situated on a knoll where a major river is forded by a trade road.

Over the years raiders from wilder lands gave the frontier settlers a hard time, and so the royal army built a series of garrison forts along the borders which are titled to retiring officers as newer forts encroach upon wilderness territories.

Governments that have been in power longer tend to collect castles because maintaining one is damned expensive, and as a central government subsumes the responsibilities and taxing authority of the lesser lords, they become the only ones capable of covering the expense. Most of the castle ruins in the world are ruins due to lack of maintenance rather than because of successful sieges.