But Djokovic rallied for an epic 4-6, 6-7 (5-7), 6-3, 7-5, 6-3 triumph over the 21st seed to avoid bombing out on the same gloomy day in Paris as women's world No 1 Victoria Azarenka.
"I played very badly, but I won thanks to my fighting spirit," said Djokovic, bidding to become just the third man to hold all four majors at the same time - and first since Rod Laver in 1969.
"He was the better player for the first two sets and I was fortunate to come through.
"But even at two sets down, I still believed I could do it and that's about the only positive I can take. It was one of those days when nothing worked."
The Serb will meet either French fifth seed Jo-Wilfried Tsonga or Swiss 18th seed Stanislas Wawrinka in tomorrow's quarter-finals.
Tsonga will resume today leading 6-4, 7-6 (8-6), 3-6, 3-6, 4-2 with a service break in the deciding set.
For a couple of hours yesterday there appeared every chance that both top seeds plus Federer would crash out in what would have been the biggest day of upsets in more than 100 years of Grand Slam tennis.
With Azarenka already eliminated, Djokovic stood two games away from defeat on Court Philippe Chatrier as Federer trailed by a set and 5-5 on Court Suzanne Lenglen.
Two hours later and order was restored, with Federer denying the gallant and entertaining Goffin a place in tennis history and Djokovic's Grand Slam tilt still intact.
"I didn't know much about Goffin beforehand, but I know him a lot better now," Federer said.
The 109th-ranked Goffin, a baby-faced 67kg lightweight who grew up idolising Federer, had been striving to become the first lucky loser to make the quarter-finals of a major after faltering in the qualifying event.
"I have no regrets, I gave it all I could," Goffin said.
- AAP