It depends on the prevalence of magic and monsters.
Yeah, this is really what it comes down to. In a low-magic world, castles work pretty much the same as always. Wizards are rare, mysterious, and not inclined to let themselves be used as artillery pieces. A bold and daring hero might tame a griffon and use it for a mount, but nobody is fielding armies of griffon cavalry. Dragons are terrifying incarnations of elemental fury; the idea of recruiting one into your army is a joke. All in all, warfare looks mostly like it did in the real medieval era.
In a high-magic world, with most powerful nations having a spellcaster corps, aerial forces on winged mounts, and so on and so forth...
Castles are still functional but require some adjustments. To ward off aerial bombardment, there would be a trend toward self-contained, roofed keeps rather than the elaborate arrangements of baileys and curtain walls seen in later-era real-world castles. In cases where there
were curtain walls, the battlements would be roofed and there would be protected passageways for defending troops to move around in. Most castles would have heavily protected "eyries" to accommodate friendly flyers.
While D&D magic can substitute for modern technology to a degree, there are some key differences. In particular, a
fireball spell lacks the "punch" of high explosives when attacking fortifications. Mid-level wizards, or lowbies with charged wands, can blast unprotected defenders off the castle walls, but actually bringing down the walls themselves takes serious mojo... even in a high-magic world, wizards capable of such spells are not common, and you probably wouldn't want to risk them on the front lines.
The biggest danger is wizards who are capable of teleporting or opening portals directly into the enemy keep, bypassing the defenses entirely and allowing invaders to pour in. To help defend against this, castles could be divided up with "bulkheads," so that a breach in one sector could be sealed off and the invaders contained--much like a submarine or a spacecraft sealing off a hull breach. Each sector would be capable of functioning independently, with its own food supplies, armory, sleeping quarters, latrines, and so forth.
(Edit: Per Celebrim, apparently most of this was standard operating procedure in the real world! Medieval armies were more sophisticated than we give them credit for.)
Of course, there is also the question of defensive magic. If it's possible and reasonably economical, anti-teleportation wards would be standard operating procedure on every substantial fortification; a couple of rooms would be exempted for the benefit of friendly wizards, but those rooms would be designed so the defenders can turn them into killing zones if hostile casters try to port in. Castles would maintain stockpiles of spell/ritual components for defending wizards.
In the extreme case, major castles would maintain a network of teleportation circles, allowing each castle to bring in thousands of defending troops at a moment's notice! This might actually be the thing that changes castles more than anything else. If every castle can be reliably reinforced via permanent portals, then there's no need to waste space on supplies or even living quarters for more than a skeleton crew... and to a great extent, no need for a castle at all. Most castles would be designed around protecting the portal itself. "Real castles" would be limited to a handful of truly massive fortifications at the heart of the realm, where the king and nobles reside with the bulk of their armies.
Then again with sufficient magic, or with cooperative magic, need castles be fixed emplacements as we normally think of them? Could they not become mobile command centers, perhaps even a type of FOB able to be transported or teleported into and out of the areas of an engagement as necessary? One could even imagine, with enough power expenditure, or trasnformative magic, a walking castle, or one that transforms into other things.
The Dragonlance novels had this with the flying citadels, which totally wrecked the defensive plans of people accustomed to more traditional modes of warfare. In fact, the whole Chronicles trilogy is a case study in what happens when you drop a high-magic army (the dragonarmies) into a low-magic world (post-Cataclysm Krynn).