How about an open-topped armored personnel carrier or infantry fighting vehicle? How about unarmored trucks, jeeps, universal carriers, motorcycles, bicycles, horses? I'm talking about the Second World War, in units for which flying hostiles were a lot more than a "possibility".
Correct me if I'm wrong, but everything you just listed there is considerably more mobile and harder to hit than a castle.
They're not enormous sitting ducks. They're mobile. They're small.
IOW, they're still useful.
In real life, the "standard RW castle" was equipped with hoardings for the ramparts. In real life, the courtyard got plenty of use during the vast majority of time when the castle was not under attack -- and did not make the fortifications around it any less useful in time of need. In real life, those fortresses had roofs, and their walls were pierced with shooting ports.
Wooden hoardings would be useless against a 200lb rock dropped by a swooping dragon, and might be a deathtrap if Flaming Sphere'd, Fireballed or DBFed, or if a Fire Elemental shows up inside...
A courtyard and its contents are a tough target for a land-based seige engine- its akin to shooting a basket in basketball. But for an airborne opponent dropping rocks (and other ordinance), they'd all be easy pickings. Proverbial fish in a barrel.
Versus a basically medieval army alone, or one accompanied by a few fantastic figures, a good old castle may do just fine.
Once one force sees the effectiveness of "special units" others will follow that lead. During WW2, both Allied and Axis forces occupied certain castles...mostly well away from the artillery and targets likely to be bombed (like factories or the main body of their forces, thanks to radio). This compartmentalization- coupled with air-forces on both sides of the battles- meant that the castles were at relatively low risk of direct attack.
Look at a "floating fortress"- an aircraft carrier. What happens to them when their planes are not airborne (for whatever reason)? They get sunk.
But in a typical fantasy world, the castle is both headquarters AND base of operations. IOW, a juicy target. Once the efficacy of an air force proves its worth, you're going to start to see the same kind of reaction as you saw in the real world- distribution of forces; trenches; bunkers.
Then consider that in most cases a castle actually serves in battle for a tiny fraction of its occupation -- if ever at all. In the meantime, it is a lord's home and court, a (or the) center of manorial society.
Consider that in a world full of air forces that we don't really reinforce any of our seats of power- a process that began as cannon and other artillery became dominant.