Federer, aiming to add to his grand slam men's record total of 17 singles crowns, survived a pair of match points when down 15-40 on serve in the 10th game of the fourth set.
Frenchman Monfils struck a backhand long to squander his first chance to claim victory and then Federer denied the 20th seed with a sizzling forehand winner to stay in the match.
From that point on, Federer took charge by holding serve for 5-5, closing out the fourth set and then breaking Monfils in the first game of the fifth.
Playing at his brilliant best after scattering unforced errors earlier in the match, the Swiss master took nine of the last 11 games in the three-hour 20-minute clash at Arthur Ashe Stadium.
Federer said he kept a positive attitude despite falling behind.
"I still thought the finish line was far for Gael," Federer told the stadium crowd. "I knew I could play better tennis.
"When I was down two match points, I wasn't feeling so great ... I thought 'this is it, this is the last point, man. Just go down fighting, don't miss an easy shot and let him have it.'
"I served well and stayed in the match and somehow turned it around. I felt great in the fifth, though. I started playing better and better as the match went on. It's a great feeling."
It marked the ninth time in his career that Federer escaped from a two-set deficit, the last time in the third round of Wimbledon in 2012 against another Frenchman in Julien Benneteau.
Federer will play 14th seed Marin Cilic, who blasted his way past sixth seed Tomas Berdych 6-2 6-4 7-6(4), on Saturday for a place in the final.