Cristiano Ronaldo opened the scoring midway through the first half but also saw a penalty saved by spot-kick specialist Diego Alves early in the second period.
Former Madrid man Dani Parejo looked to have handed Valencia an unlikely point with a stunning free-kick eight minutes from time.
Real stormed forward and needed just four minutes to find the winner when Marcelo cut inside and fired into the far corner.
Real now lead Barcelona by three points at the top of the table with four games to go for both sides.
Barca, though, can move back to the summit of La Liga with victory in the Catalan derby later at Espanyol.
Madrid boss Zinedine Zidane accepted his side nearly paid the price for taking their foot off the gas at 1-0.
"It is true that we were in a comfort zone for 30 minutes, the fans were the same," said the Frenchman.
"After Valencia scored they started to push the players on once more and it was a completely different match."
The Brazilian Marcelo added: "We put our heart and soul into it and showed who we are.
"With the fans behind us we are hard to beat here."
Zidane recalled eight of the side that started last weekend's 3-2 Clasico defeat to Barcelona and had been rested for a 6-2 thrashing of Deportivo la Coruna in midweek.
However, the habitual first team nearly got off to the worst possible start.
Santi Mina struck the post inside the first minute before Fabian Orellana also screwed a great chance wide for Valencia.
However, Ronaldo punished their profligacy in characteristic fashion as he bulleted Dani Carvajal's cross into the far corner on 27 minutes.
That goal took Ronaldo's tally in league football to 367 goals to surpass legendary English striker Jimmy Greaves in Europe's top leagues.
However, even the four-time world player of the year struggles to beat Alves from the spot.
Madrid were awarded a penalty when Luka Modric went down under slight contact from Parejo 11 minutes into the second half.
And for the third time in four penalties against Alves, Ronaldo was denied by the Brazilian.
Ronaldo then missed an even easier chance as he bundled over and then crashed into the goalpost from Marco Asensio's enticing cross.
Those misses looked costly when Parejo's dipping free-kick set up a grandstand finish.
Yet, for the seventh time in La Liga this season, Madrid struck late to maintain the upper hand in the title race as Marcelo used his weaker right foot to fire across and past the helpless Alves to joyous scenes of relief at the Santiago Bernabeu.
Next up for Real is a Champions League semi-final first leg on Tuesday at home to a Atletico Madrid side full of confidence after they thumped Las Palmas 5-0 to move three points clear of Sevilla in third.
Atletico struck three times in the first 18 minutes in the Canary Islands as Kevin Gameiro netted a double either side of Saul Niguez's header.
Kevin-Prince Boateng was then sent off for Las Palmas and Atletico made their man advantage count with further goals from Thomas Partey and Fernando Torres.
Tony Adams's Granada were relegated after they lost for the fourth straight game since the Englishman took charge, going down 2-1 at Real Sociedad.
Meanwhile, Levante sealed promotion back to La Liga with a 1-0 win over Real Oviedo.
The agreement is the first bilateral trade deal between the Gulf and South America
Around 320 of 760 flights planned for Saturday had been cancelled so far