Federer's bid to win a ninth Wimbledon title came to an astonishing end as the Swiss star collapsed in a nail-biting four hour and 13 minute classic that ranks as one of the tournament's greatest upsets.
It was Federer's earliest departure from Wimbledon since his shock second round defeat against Sergiy Stakhovsky in 2013.
Eighth seed Anderson will play 2016 runner-up Milos Raonic or American ninth seed John Isner on Friday for a place in Sunday's final.
“Down two sets to love I tried my best to keep fighting. Beating Roger Federer here at Wimbledon will be one I remember, especially in such a close match,” Anderson said.
“I kept telling myself to keep believing. I said today is going to be my day. You need that mindset against Roger.
“I'm ecstatic. That's what you work so hard for. Matches likes that are very special.”
For the only the second time at Wimbledon, Federer was beaten after holding a two-set lead, with his previous loss from that position coming against Jo-Wilfried Tsonga in the 2011 quarter-finals.
The 20-time Grand Slam champion appeared to be moving towards his fifth successive Wimbledon semi-final after taking the opening two sets.
That initial burst gave Federer 34 successive sets won at Wimbledon, equalling his own record set between 2005 and 2006.
But, playing on Court One for the first time in three years, Federer was unusually error-prone and became visibly frustrated as eighth seed Anderson refused to surrender.
Anderson had failed to take a single set off Federer in their previous four meetings.
Yet once he had ended Federer's run of holding serve for 85 consecutive games — a streak dating back to last year's semi-final — Anderson's confidence soared.
Deploying his huge serve to lethal effect, Anderson roared back to leave the champion's title bid in tatters thanks to a barrage of 28 aces and 65 winners.
Anderson is the first South African man to make the last four at Wimbledon since Kevin Curren in 1983.
The 32-year-old made his maiden Grand Slam final appearance as last year's US Open, losing to Rafael Nadal.
But his run at Wimbledon might have come as a surprise even to Anderson, who had never made it past the fourth round prior to this year and lost in the first round of the grass-court event at Queen's Club last month.
Not since Wayne Ferreira in 1994 had a South African man made the Wimbledon quarter-finals, but Anderson wasn't overawed by the occasion.
Federer didn't seemed bothered by being away from Centre Court when he cruised through the first set in just 26 minutes.
But he lost his rhythm at the start of the second set as Anderson made his move.
Federer responded, quickly breaking back before taking the set in a tie-break.
But Anderson was just getting started.
He saved a match point at 4-5 when Federer missed a forehand, then broke to serve for the third set.
Federer was completely out of sorts by now and his uncharacteristic struggles on his forehand didn't improve in the fourth set.
Anderson scented blood and, out-duelling Federer time and again, he broke in the seventh game to level the match at two sets all.
Federer couldn't convert a break point at 3-4 and as the final set headed into extra time a lone voice in the crowd, mindful of England's imminent World Cup semi-final against Croatia, shouted out “Hurry up I need to watch the football”.
Only once before had Federer played more games at a Grand Slam and on that occasion, he prevailed 16-14 in the 2009 Wimbledon final against Andy Roddick.
But this time Federer cracked, serving his first double fault at 11-11 to give Anderson the crucial break that ushered the Swiss legend to the exit door.