This comeback was even more dramatic than his third-round victory over John Millman, where Federer recovered from 8-4 in the deciding tie-break.
Here, he was clearly physically below his best, taking an off-court medical timeout in the third set, but saved seven match points in the fourth on his way to a 6-3 2-6 2-6 7-6 (8) 6-3 victory.
Federer said: "Got to get lucky sometimes I tell you that. Those seven match points, you're not in control. I was just hoping he was not going to smash the winner, and if he missed one or two, who knows what he is thinking about.
"I got very lucky. As the match went on I started to feel better again and the pressure eased off.
"I don't deserve this one but I'm standing here and I'm very very happy."
Federer was struggling to move to his forehand side and looking nothing like the player who has won 20 grand slam titles.
American Sandgren, ranked 100 and playing in the biggest match of his life, had three chances at 5-4 in the fourth set but made unforced errors on all of them.
He then led 6-3 in the tie-break and had a further chance but Federer, who has never retired from a professional match, simply refused to be beaten.
He loosened up a little and forced a set point on his own serve, wagging his finger as Sandgren sent the ball flying long.
And Federer ensured those heroic efforts were not in vain, freeing himself up to win the fifth set after three hours and 31 minutes.
The 38-year-old seemed to have had his major scare against Millman, although he was still far from his best against Marton Fucsovics in round four, fighting back from a set down.
Both those matches were played at night, and the sunshine seemed to help Federer as he eased to the first set against Sandgren, who was looking to become the lowest-ranked man to reach the semi-finals here for nearly 30 years.
But Federer's problems began when he dropped serve in the second game of the second set, his usually-reliable forehand breaking down with alarming regularity.
He was furious to receive a warning in the third game of the third set after a line judge reported him swearing, but that was a trivial matter compared to what was going on with his body.
The Swiss revealed afterwards he was struggling with a tight groin, and it seemed highly unlikely he would be able to fight through it.
He said: "It's been a lot of tennis throughout my life. I was starting to feel my groin, my leg was tightening up.
"I don't like calling the trainer, sign of weakness and all that. I believe in miracles. It wasn't bad enough where I thought it was going to get worse. Let him finish me off in style and he didn't do that."
Next up for Federer is the winner of the clash between his great rival Novak Djokovic and Milos Raonic later on Tuesday.
The semi-final will take place on Thursday and the 38-year-old Swiss said: "You do feel better in a couple of days and you just never know.
"With these lucky escapes maybe you play freely because you know you should already be skiing in Switzerland. Might as well make the most of it."