Solo creatures can be hard to balance. The encounter math says 5x PC20s is looking at a Hard encounter and 5x PC19 is Deadly. IMO boss battles should be at a minimum Hard, with the possible exception that you already threw several waves of minions to exhaust the party.
Consider the realities of your fight. Dragons are flying creatures so there should be room for a huge dragon to spread their wings and get to the sky. By the same token, it may be sufficiently small that their breath weapon can cover the entire area the dragon doesn't fill.
There should be no useful cover. Any nooks or crannies in the lair should be trapped or some kind of trick the dragon has prepared.
Dragons have 60ft blindsight so use it. Fill the lair with fully obscuring smoke. Casters can't hit targets they can't see and martials will be at disadvantage. Meanwhile the dragon gets to attack with advantage.
Using a CR24 Ancient Red as an exemplar, it gets the fear presence , the "normal bite + 2x claws" multi attack (or a breath weapon), one free AoE lair attack each round plus 3 legendary actions.
The dragon has treasure and let them use it. Other heroes probably brought ice weapons to use on a red dragon, which are also good for a red dragon to use on heroes with fire resistance spells. Other nice things are items of Dispel Magic or AntiMagic to strip away those immunities.
Avoid wasting actions on potions of healing until the dragon is fleeing. However a potion of polymorph into a giant ape so it can make ranged attacks (and force PCs to waste resources) to assess the PCs. Potions of haste to let them cast a spell then breath fire plus a bit of AC. Potions of Cold Resistance.
Ancient reds get spellcasting: bane, dominate person, heat metal, hypnotic pattern, power word stun, suggestion each 1/day with save dc 21. Spells aren't a great choice vs melee attacks but can be used strategically. Bane before fear or breathing fire. Suggestion "run for you life" after savaging a PC. Dominate Person to make a caster Dispel Magic on allies or themselves.
I will recommend against spreading out attacks, aside from AoEs. Focus on one physically weaker PC and beat them down. Don't go for the fighter, eat a Wizard or rogue. Force the cleric to spend spells on healing the victim. In theory tit can get 7 attacks on its chosen foe per round (with advantage) for ~100hp/round which should terrify a squishier PC.
Feel free to have a pool of boiling hot water that is a few feet deep. Let the dragon grapple a caster, chew on it, squeeze it, swat it several times, then submerge them. Maybe add a Glyph of Warding on the pool that casts AntiMagic so that water breathing and fire resistance is removed. Trade one claw for boiling/drowning damage plus eliminating a caster's spells.
Glub-glub.
The dragon should be okay with fleeing. If it gets to half hit points, flee. After all, who says the lair fight should be the,final fight? Why not have that be the halfway point in the overall adventure where they hunt each other?
Maybe when it flees it triggers a trap that drops lava into the cavern. Can the PCs escape before they are crushed/incinerated/suffocated?
Play hit and run for days. Drop boulders from high altitude. Fly up, grapple a PC and run away with them, chewing and crushing as they go. Wait for the PCs to go home and set a whole city on fire, possibly by dropping flaming trees onto the city so the dragon isn't in range. Fly by the farms and Fear the animals so they cripple themselves running away.