After fighting back from match point and love-40 on his serve to take the third set, the South African broke Federer in the seventh game of the fourth set.
Federer was on the ropes after dropping a comfortable two-set lead, and he even failed to take a match point chance in the third set, but the South African held his nerve to win a captivating contest 2-6 6-7 (5-7) 7-5 6-4 13-11.
Bumped to Court 1 for the first time this tournament, Federer suffered away from the home comforts of Centre and missed a match point in the third set.
The 2017 US Open finalist will meet Canada's Milos Raonic or American John Isner in his first SW19 semi-final. That's the mindset you need against someone like Roger.
"So it's an important moment", said Nadal, the 17-time Grand Slam champion who has made four successive quarter-finals at the majors for the first time in six years.
It means Nadal is now in his 35th Grand Slam quarterfinal and following previous years of injuries and bad luck at SW19, the Spaniard was pleased to reach the last eight.
Asked if he expected more from an opponent in the world's top 30, Federer said: "I think it's a tough matchup for him maybe against me".
Anderson stayed cool and sealed victory on his first match point with a powerful first serve which Federer could only return into the tramlines.
Federer and Nadal are favourites to meet in Sunday's final - 10 years after their epic 2008 showdown - but Wilander believes Del Potro could throw a spanner in the works.
And it was Federer who blinked, double-faulting to face a break point at 11-all, then slapping a forehand into the net to cede the key break.
It was Federer's earliest departure from Wimbledon since his shock second round defeat against Sergiy Stakhovsky in 2013.
The only other times he has surrendered a two-set lead came against Lleyton Hewitt in Switzerland's 2003 Davis Cup semi-final loss to Australia in Melbourne, to David Nalbandian in the 2005 Masters Cup decider.
Federer/the Fed-express/the GOAT, has glided effortlessly into the Wimbledon quarter-finals, a place he knows better than most people know their own back garden.
Tennis legend Roger Federer has been hailed for a ideal batting technique after he showed off his cricket skills at Wimbledon.
There was little sign of the drama which unfolded when Federer raced through the first set in 26 minutes.