Luka Modric struck the crossbar midway through the first half but Real were frustrated by an aggressive and disciplined Getafe team who deserved the victory, just their fourth in LaLiga this season.
Real manager Carlo Ancelotti called on Eden Hazard and Marcelo at half-time but they continued to struggle and created few chances. Karim Benzema was kept quiet and Real missed Vinicius Junior, who was unavailable after testing positive for COVID-19 this week.
The result, Real's first defeat since they were beaten by Espanyol on Oct. 3, means the LaLiga leaders stay eight points clear of second-placed Sevilla in the table, although Julen Lopetegui's side now have two games in hand and can reduce their advantage with a win at Cadiz on Monday.
Real have now also dropped five points in their last three LaLiga games following their 0-0 draw against Osasuna on Dec. 19.
They welcomed back a number of first-team players into their starting lineup following a COVID-19 outbreak, including Modric, Marco Asensio, Rodrygo and Courtois, who tested negative following his positive result on Wednesday.
Ancelotti remained without Vinicius, however, after his positive test this week and Asensio was preferred to Hazard on the left of Real's attack.
Getafe made an aggressive start at the Coliseum and striker Unal capitalised on Militao's loose touch before sliding the ball past Courtois.
Real were rattled and a further mistake from Courtois handed Getafe a golden chance to double their lead. The goalkeeper passed the ball straight to Nikola Maksimović, who couldn't find the empty net from 25 yards with Courtois stranded.
Modric then forced Getafe goalkeeper David Soria into an excellent stop with a curling effort from distance that needed to be tipped around the post.
It sparked Real into life and Modric went close again as he rattled the underside of the bar with a left-footed effort before Toni Kroos pushed a half-volley wide from Lucas Vasquez's cross.
Rodrygo and Ferland Mendy had chances, only to see the play pulled back by the referee's whistle, and Real's frustration during the opening period was illustrated by Ancelotti being shown a yellow card for his protests on the sideline.
Getafe almost went into the break two goals in front only for Carles Alena's goal-bound effort to be blocked by Militao on the stroke of half-time.
Ancelotti made two changes at the interval with Hazard and Marcelo replacing Asensio and Mendy but it took 20 minutes into the second half for Real to threaten, with Hazard and Marcelo combining to set up Benzema, who saw his shot blocked by Jorge Cuenca.
Soria made an excellent stop to tip Casemiro's volley around the post as Real began to turn up the pressure late on.
The 19-year-old Peter won a late free-kick that Isco directed on target with a rising header, but Soria was equal to it as the match entered seven additional minutes of stoppage time.
But a loose pass from Benzema that rolled straight out of play in the dying seconds summed up Real's afternoon as Getafe celebrated a famous win.
"A difficult game, a tough game," Casemiro said afterwards. "We have to be fair, we have to say it was an individual error, we all make mistakes, but it was a player [Militao] who has been one of the best in the team recently, but that's football. You have to be careful. We fought and we tried, but we have to change this, we have to think about the cup game now.
"It's not just Getafe. We're having some problems when we play against a line of five at the back, we haven't got three points in those games, so we have to work and keep working."