The South African dug deep after warding off match-point and broke the Swiss in the 11th game of the third set before closing out with an ace in the 12th.
The comeback was just beginning. I feel like I'm getting better and better with every match.
Djokovic and Nishikori will play the first match on Centre Court, followed by arguably the pick of the quarter-finals between Nadal and Juan Martin del Potro, meaning Federer must make a rare visit to Court One.
"It's disappointing losing the next two sets after winning the first two and having match points", said Federer.
How hard was it to see this coming?
But the 32-year-old Anderson refocused in the second set to end the Swiss star's streak of consecutive games won on serve at 85 as he raced into a 3-0 lead before Federer quickly hit back to win the next three. But there was more.
It was a match Federer could, probably should, have won and yet he will now leave here wondering - probably like everyone else - how many more chances he will get to add to his eight Wimbledon titles.
The shock of the tournament appeared to be edging closer when Anderson moved 30-0 up in the third game, but Federer relied on his serve to salvage a hold, doing the same four games later to dig himself out of another hole after Anderson had also recovered from 30-0 down.
"I just kept on telling myself "I have to keep believing". Switzerland's Roger Federer in action during his quarter final match against South Africa's Kevin Anderson. He knocked on the door by making it to the French Open's fourth round, and he now seeks out his first Grand Slam title.
When Anderson broke again in the fourth, Federer was suddenly on the ropes.
Rather than simply guiding the bouncing ball away and towards one of the ball kids, Federer thought it would be a good opportunity to provide some bonus entertainment for his adoring fans at Wimbledon.
Additionally, if you can't watch live, you can get 50 hours of cloud DVR storage as an additional add-on when signing up for your free trial.
Anderson then served it out, ending things with a 205kph service victor, and raised both arms overhead.
The eighth-seeded Anderson advances to face either No. 9 John Ishner or No. 13 Milos Raonic in a Friday semifinal match.
Neither player had been able to break the other one in the fifth set, with each holding their serve, until Anderson did to go up 12-11.
Anderson trails Isner 3-8 in career meetings.
DJOKOVIC THROUGH Novak Djokovic, the three-time champion, showed Centre Court's capacity crowd a fair reflection of the player he was two summers ago. Reaching his first grand slam final at the US Open a year ago, where he lost to Nadal, gave him great confidence and it was noticeable even early on that he was able to push Federer back, a rare sight, especially on grass.