Madrid came into the match in the spotlight having picked up just one point from their away games at Levante and Racing Santander and the title hopefuls could hardly have had a worse start as poor defending saw them trailing after just 14 seconds, Miguel Perez Cuesta the scorer.
Madrid seemingly put that dismal beginning behind them with three goals in 13 minutes either side of half-time through Ronaldo (two) and Gonzalo Higuain, but the hosts soon found themselves with more problems.
Rayo pulled a goal back in the 55th minute and then two minutes later Madrid lost Angel Di Maria to a second yellow card.
That left Madrid facing a potentially crucial final half-hour with a man less, knowing more dropped points - especially at home - could be decisive come the end of the season.
However, it was Jose Mourinho's men who seemed to adapt better to the dismissal as they netted three more times to take the win, Raphael Varane, Karim Benzema and Ronaldo - with his second penalty of the match to complete his hat-trick - the goalscorers.
Mourinho had dismissed talk of crisis ahead of the game but he knew Madrid could ill afford to slip up again after taking just one point against Levante and Racing and failing to find the net in either game.
But the Portuguese's confidence was beginning to look a little misplaced as Rayo took the lead with the first attack.
Madrid midfielder Lassana Diarra gifted possession away inside his own half and, after Casillas had pushed Raul Tamudo's shot straight back out, Cuesta slotted in the rebound.
Rayo came close to further embarrassing their hosts in the 14th minute when Tamudo's shot was parried by Casillas, before Madrid finally started to get going.
Mesut Ozil, who had replaced Diarra in the 29th minute, brought a good save out of Dani Jimenez before Ronaldo drew the teams level with a low left-footed shot through Jimenez's legs.
Then, in first-half stoppage-time, Higuain made it 2-1, the Argentinian, who had seen a goal ruled out for offside, tapping into an open goal after Sergio Ramos had beaten Jimenez to Xabi Alonso's free-kick.
Michu gave Madrid a scare at the start of the second half when he almost pulled a goal back, but when Ronaldo made it 3-1 in the 51st minute from the penalty spot after Kaka had been fouled by Jose Movilla, the home side's troubles looked behind them.
However, a two-minute spell soon after but the result back in doubt.
Firstly Rayo made it 3-2 when Cuesta slotted in at the far post from a corner , and then moments later Di Maria was handed his marching orders following an intentional handball near the touchline.
Mourinho responded to that by replacing Kaka and Higuain with Benzema and Sami Khedira, and the reshuffled Madrid line-up quickly took the game by the scuff of the neck.
Alonso saw a free-kick tipped over but from the resulting corner summer signing Varane somehow managed to turn Ozil's cross into the roof of the net with his heel after appearing to have overrun the ball.
Benzema then slotted in Madrid's fifth from the edge of the area, before Ronaldo completed the scoring in the 83rd minute with a dinked penalty down the centre of the goal after he had been tripped trying to round Jimenez.
That was the £80million man's second treble of the season, to add to the seven he got last term.