If castles have a single theme then it is false security. A castle implies a seige, an unscalable wall implies a way up, an impenetrable keep implies a way to sneak in. A castle is a challenge and a castle of any significance in a book is like Chekhov’s gun — if it isn’t besieged or infiltrated then it may seem superfluous.
Isengard, Helms Deep, Minas Tirith in The Lord of the Rings are all besieged. Minas Morgul is not infiltrated as such but the heroes sneak past its defences. Mordor itself, essentially a whole territory that is a fortress walled on three sides with a mighty gate and an inner keep, is infiltrated by Frodo and Sam.
The castle of the enemy is a place to escape from, burgle or rescue a character from. The might and invulnerability of the enemy can be subverted — a weakness found, an entry point, someway in.
But castles are also where people live and specifically aristocrats live, lords, ladies and kings and queens. So a castle overlaps with a palace. I’ll discuss palaces another time but they carry with them the notion of false security. A castle (or a palace) has guards and walls but it is also a place of assassins, secret passages, intrigue and betrayal and as such the theme of false security is common to both.
Castles (and canonically palaces) overlap with labyrinths in that they are places of confusing passages. They can also harbour monsters. Dracula’s castle or Frankenstein’s (in films) evoke labyrinths more than castles in that house monsters but like castles proper, they necessarily fail in providing safety — as a castle in a story can no more constrain a monster it encloses than it can prevent a siege. A castle is not just a false promise to keep things out but a false promise to keep things in (see also Michael Mann’s odd and pretentious film The Keep for another castle with a monster in the middle).
Like the eponymous ‘Castle Doctrine‘ in US law, a castle marks a fear of invasion and the use of violence to prevent it. That fear may be justified but castles can represent consuming fears that mark paranoia or literary madness. Saruman and Denethor sit in the strongest keeps of the fortresses slowly eaten inside by fear (fueled by Sauron via a palantir). Likewise, in The Silmarillion, when Feanor is consumed by paranoia because of the lies of Melkor, he first retreats to his fortress of Formenos♦.
For places that in reality would be busy mini-towns crammed full of people, castles and fortresses can carry with them connotations of inward-looking retreats. Superman’s Fortress of Solitude doesn’t fit the other aspects of castles I’ve listed above but overtly expresses this idea of a fortress as a personal retreat. In a similar way, Howl’s Moving Castle reflects the wizard Howl’s self-absorption and emotional defences in both the film and book*.
The castles of Britain also marked historical fear of the population — Norman edifices to assert dominance over the population and later English/Norman edifices in Wales and Scotland to assert control of territory. The castle can both embody fear of the outside and project fear and dominance outward. The Death Star in Star Wars maybe technolically and militarily absurd but it captures this dual nature of the castle as a political expression well. It embodies the Emperor’s inate fears and also serves to intimidate the subjugated population of the former Republic. Of course, because the Death Star is a conventional castle in all aspects aside from size, shape and location, it ends up both infiltrated and besieged and its defences overcome.
A castle might be a refuge but I mean something conceptually different. A refuge is a place that offers temporary safety for characters. A refuge and a castle both offer protection but a refuge does not offer false safety. The safety a refuge offers is limited (because otherwise, the story can’t continue) but it is not false. The limitation may be straight-forward i.e. it is a place that can only offer limited defence against the forces or it may simply be limited by the protagonist’s objectives. Tom Bombadil’s house in Lord of the Rings, like Bombadil himself, maybe supernaturally safe but the hobbits can’t remain there because of their own objectives. Likewise, Rivendell and Lothlorien, while not invulnerable can only offer temporary protection because true safety can only be achieved by Frodo completing his quest.
Tolkien made both Rivendell and Lothlorien effectively fortresses in the sense of heavily defended and defensible places but also made them places that don’t physically resemble castles. I presume that he did this to characterise their elvish aspect (in the Silmarillion there are more overtly castle-like elvish cities, Nargothrond and Gondolin, both of which are destroyed despite their impressive fortifications).
A refuge can offer limited safety by being hidden. Akira Kurosawa’s film of the same name refers to the temporary hiding place of a general and princess of the defeated Akizuki clan. As with any refuge, the hideaway can only be temporary. Star Wars, that liberally borrowed from Kurosawa, repeatedly makes use of hidden rebel bases that offer short-term safety to the protagonists.
An awareness of the vulnerability of your hiding place is treated as a virtue of character. To know that your fortress can only hold back your enemies for so long is treated as wisdom. A story about a wise king who had carefully ensured that his impregnable fortress really was impregnable and who had taken great care to ensure that it had more than enough food and water to outlast any feasible siege is not inherently appealing. Instead, the quest has to keep going or the military hero has to take the fight to the enemy. Notably, Aragorn does this twice. In the siege of Helms Deep, he resolves to make a last-ditch sortie out of the keep. Later, having raised the siege of Minas Tirith he takes his forces out of the city to attack the Black Gate of Mordor to misdirect Sauron. Castles are cool but keeping your hero (or your murderous monster) stuck inside is unwise for a story.
♦[ In another case of synchronicity post-writing this: “Elon Musk is building a medieval watchtower” https://www.cnet.com/news/elon-musk-is-building-a-medieval-watchtower-the-boring-company/ ]
*[I did read the book once but I know the film better, which is quite different]