Raonic beat the seven-time champion 6-3, 6-7 (3), 4-6, 7-5, 6-3 on Centre Court, handing the Swiss great his first loss ever in the Wimbledon semifinals after 10 straight wins.
"This one clearly hurts because I felt I could have had it," Federer said. "So close. It was really so, so close. It clearly hurts."
In Sunday's final, Raonic will face 2013 champion Andy Murray, who swept Tomas Berdych 6-3, 6-3, 6-3 in less than two hours to reach the championship match for the third time at Wimbledon and 11th time at a Grand Slam.
The second-seeded Murray has a 6-3 career record against Raonic, including a win in the final of the Wimbledon warm-up at Queen's Club last month. But this is Murray's first Grand Slam title match against someone other than Federer or Novak Djokovic.
"You learn from those matches, for sure," said Murray, who's 29. "The older you get, you never know how many chances you're going to have."
The 25-year-old Raonic became the first Canadian man in history to advance to the final of a Grand Slam tournament. The only other Canadian to get this far was Eugenie Bouchard, the women's runner-up at Wimbledon in 2014.
The sixth-seeded Raonic avenged a Wimbledon semifinal loss to Federer in straight sets two years ago. The No. 3-seeded Federer broke serve only once, while Raonic managed three breaks.
"An incredible comeback for me, really," Raonic said. "I was struggling there through the third and fourth sets. He was playing some really good tennis. And just on the little, little opening, I managed to turn it around.
"I'm by no means done what I want to be here to do," said the 6-foot-5 (1.96-meter) Canadian, whose serves reached 144 mph (231 kph) and who averaged 129 mph (207 kph) on first serves -- 3 mph (4.8 kph) faster than Federer's fastest of the day.
For Federer, the loss means he remains one title short of the all-time Wimbledon men's record. He's still tied with Pete Sampras and 1880s player William Renshaw with seven.
Federer last won Wimbledon in 2012, the most recent of his record 17 Grand Slam championships. He lost in the Wimbledon final the past two years to Djokovic, who was ousted in the third round this year by Sam Querrey.
"I mean, yes, while I'm in the tournament, it's a dream to win my eighth," Federer said. "It's not my only reason why I play tennis, just to be clear. Otherwise I'll go in a freeze box now and come out before Wimbledon next year."
The match turned in the fourth set, as Raonic saved three break points -- two in one game to stay ahead 3-2, another with a service winner to hold for 5-4.
Then Federer went up 40-0 on serve at 6-5 but couldn't hold. He served two double-faults in a row to set up deuce. Federer saved two set points before Raonic hit a backhand passing shot down the line to send the match into a fifth.
"I can't believe I served a double-fault twice," Federer said. "Unexplainable for me really. Very sad about that and angry at myself because never should I allow him to get out of that set that easily."
After that break, Federer called for a trainer on the changeover and had his right thigh massaged.
Then, while serving at 2-1 down in the fifth, Federer lost his footing on a deuce point and fell onto his stomach on the grass while trying in vain to reach a passing shot. Federer went immediately to his chair and called for trainer, who examined his left knee.
Federer, who had surgery on his left knee in February, resumed the game and didn't show any outward sign of injury. But he was broken in that game after another crucial double-fault at deuce. On the second break point, Raonic stroked a forehand cross-court passing shot winner.
That break put Raonic in firm control. He stayed on top the rest of the set and served out the match at love.
Federer said later he wasn't sure whether he had picked up an injury.
"I hope I didn't hurt myself," he said. "I don't know at this point. I hope it's not so bad. I walked it off. I was able to finish. But I don't slip a lot. I don't ever fall down. It was a different fall for me than I've ever had.
"I just hope I'm going to be fine. I believe I am, but I'll know more tomorrow when I wake up."
It was a breakthrough victory for Raonic, who has had John McEnroe in his coaching corner since the Wimbledon warm-up tournament at Queen's Club.
"What he told me today is: 'Go out there, leave it all out there,'" Raonic said. "I showed a lot of emotion out there, always positive, and I think that's what got me through. Mentally, I had one of my best matches in my history and my career."
Raonic takes down Federer in semis
Milos Raonic defeats Roger Federer 6-3, 6-7, 4-6, 7-5, 6-3 to advance to the Wimbledon final.
Raonic proud of 'incredible comeback'
Milos Raonic reacts to defeating Roger Federer in five sets to advance to the final at Wimbledon.
Cahill: Raonic took semifinal away from Federer
Darren Cahill and Chris Fowler break down how Milos Raonic was able to take down Roger Federer in the semifinals at Wimbledon.