The 33-year-old opened his account after just 11 minutes to give Madrid the lead, but Girona - who produced the biggest result in the club's history with victory in the reverse fixture - went into the break all square courtesy of Cristhian Stuani's equaliser.
Ronaldo went on to add two more either side of a Lucas Vazquez strike in the second half to seemingly wrap up the points, but Girona refused to go down without a fight and fought back through Stuani's second of the night and 17th of the season.
Gareth Bale came off the bench to make it 5-2 with just four minutes of normal time remaining, only for Girona to again valiantly fight back on their first ever visit to the Bernabeu as Juanpe reduced the deficit back to two moments later.
However, it was fittingly Ronaldo who had the last say with his fourth of the night in stoppage time to finally shake the visitors off in a nine-goal thriller.
The result lifts Madrid back up to third in the La Liga table, and crucially sees them close the gap on Atletico Madrid back to four points after their city rivals fell to a late defeat at Villarreal earlier on Sunday.
Madrid boss Zinedine Zidane opted to leave the likes of Bale, Luka Modric and Casemiro on the bench for the visit of La Liga's surprise package, but the hosts quickly took control of possession and threatened to take the lead after only five minutes when Ronaldo's powerful free kick was tipped over the crossbar.
An even better chance went begging for the defending champions moments later when a failed clearance fell kindly for Karim Benzema, who teed up Vazquez only for the midfielder to fire straight at Yassine Bounou when he only had the keeper to beat.
Girona had their first sight of goal in the 10th minute when Borja Garcia cut inside and drew a diving stop from Keylor Navas, but within one minute Madrid were back up the other end and breaking the deadlock.
Toni Kroos played a short corner before getting the ball back and delivering a low cross into the box which Ronaldo met with a well-controlled first-time finish to sweep the ball into the far corner for his first of the night.
The early goal was no surprise considering only one team had conceded more inside the opening 15 minutes than Girona this season, whereas only one team had scored more than Real Madrid in the same period, but the visitors almost produced a quick response when Portu - scorer of the famous winning goal in the reverse fixture - towered above Marcelo at the back post and planted his header narrowly wide.
Madrid were soon back on the front foot, though, and Ronaldo skewed one effort comfortably wide of the target before Bounou was called into two routine saves to deny Marco Asensio and Vazquez.
Vazquez did have the ball in the back of the net midway through the first half when he latched on to a cleverly disguised pass from Kroos and tucked his finish into the bottom corner, only for the goal to be wrongly disallowed for offside.
Madrid's sense of injustice was almost compounded when a defensive error allowed Portu to dart into the box past a couple of defenders, but he could not poke his effort on target with only Navas to beat.
Portu chased down his own shot to win a free kick on the left side of the field, though, and that provided the equalising goal for Girona as Stuani met Alex Granell's delivery with a thumping header for Girona's first ever goal at the Bernabeu.
The goal also ensured that Real Madrid's poor defensive record stretched to just one clean sheet in their last 14 outings, but they quickly went in search of a second to regain their lead and Ronaldo almost provided it with a dipping strike which threatened to squirm away from Bounou's grasp.
Ronaldo surrendered his free kick duties to Asensio with just under 10 minutes remaining of the half, and it proved to be a good decision when the Spaniard's curling effort forced another flying save from the Girona keeper.
It was five-time Ballon d'Or winner Ronaldo who was consistently Madrid's biggest threat, though, and he tested Bounou with a deflected free kick before ending the first half with an acrobatic effort which he failed to direct on target.
Ronaldo picked up where he left off at the start of the second half, though, and it took just two minutes for him to restore Madrid's lead after the restart when he latched on to Benzema's through-ball and fired an emphatic left-footed finish in off the underside of the crossbar.
The Portugal talisman then turned provider for Madrid's third shortly before the hour mark, teeing up Vazquez who passed the ball into the bottom corner at the end of a free-flowing team move.
Nacho was the next to come close when he met Asensio's whipped delivery with a towering header, but his effort came off the crossbar as Madrid threatened to turn a previously tight encounter into a rout.
The hosts did get their fourth goal moments later, though, as Ronaldo completed his hat-trick by thumping a rebound home from close range after Benzema had missed a gilt-edged chance of his own.
It was Ronaldo's 44th hat-trick for Real Madrid and the 50th of his illustrious career, but that was far from the end of the action as both teams continued to attack at every opportunity.
Ronaldo is now one of only three players to have outscored former Middlesbrough striker Stuani in La Liga this season as the Uruguayan doubled his personal tally for the night just three minutes after seeing his side fall three behind, once again beating Dani Carvajal to an aerial ball into the box and diverting his header past the wrong-footed Navas.
Madrid had taken their foot off the pedal following their fourth goal and that almost allowed Girona to threaten a famous comeback which would have eclipsed even their win in the reverse fixture, with Stuani missing a golden chance to join Ronaldo in the hat-trick ranks when he could not get enough on his header from Granell's deep free kick.
Moments later Navas was left scrambling following a defensive error which almost gifted the visitors a third, but Bale finally looked to have put the game to bed in the 86th minute when he latched on to Modric's pass before sweeping his finish past Bounou.
The plucky visitors once again refused to give in, though, and Portu immediately drew a good save from Navas before Juanpe rose highest from the resulting corner, taking advantage of more lax defending to plant his header beyond the keeper.
It was Ronaldo's night, though, and Madrid's record goalscorer added another to his ever-growing tally in the first minute of stoppage time when he steered a pass from Kroos into the bottom corner for his 21st goal in his last 11 games across all competitions - a run which now includes six braces, one hat-trick and this four-goal haul.
That may have capped off the scoring on a goal-crazy night at the Bernabeu, but typically Girona fought until the last and almost added another twist late on when Michael Olunga's powerful drive needed to be turned around the post by Navas.
The result leaves Girona still seventh in the table and fighting for European football in their first ever season in the top flight, whereas Madrid move back above Valencia into third and close the gap on second-placed Atletico down to four points.
The champions remain 15 points adrift of runaway leaders Barcelona, though, despite stringing together a fifth consecutive home win for the first time since November 2016.
REAL MADRID (4-4-2): Navas; Carvajal, Varane, Nacho, Marcelo; Vazquez, Kovacic (Modric, 71'), Kroos, Asensio (Bale, 70'); Benzema (Isco, 83'), Ronaldo
GIRONA (3-4-3): Bounou; Ramalho, Espinosa, Juanpe; Maffeo (Aday, 83'), Pons, Granell, Mojica; Portu, Stuani (Olunga, 87'), Garcia (Lozano, 69')