The top-ranked Swiss player was pushed to the limit in a Grand Slam final for the first time, but he held on to win 7-6 (7), 4-6, 7-6 (3), 2-6, 6-2 for his 11th major title.
Federer is the first man to win five straight titles at the All England Club since Borg did it from 1976-80. The Swede watched the match from the Royal Box with other past champions, and applauded as Federer fell to the ground after an overhead smash on match point.
"It was such a close match," Federer said. "I told Rafa at the net he deserved it as well. I'm the lucky one today."
• Click here to view photos.
Federer stretched his record grass-court winning streak to 53 and his Wimbledon winning streak to 34. He is tied for third on the career list with Borg and Rod Laver at 11 major titles, trailing Pete Sampras' 14 and Roy Emerson's 12.
"Each one is special, no doubt," Federer said. "To hold the trophy is always the best thing."
After leaving the court wearing his white sport coat with the gold "RF" badge on the breast pocket, Federer and Borg hugged and visited inside the stadium.
Federer beat Nadal for only the fifth time in 13 meetings. The Spaniard has defeated Federer in the past two French Open finals to spoil his bid to complete a career Grand Slam.
"Five titles in a row, so, fantastic," said Nadal, who also lost to Federer in last year's final. "Well, anyway, I lose today, but I play great two weeks."
Federer saved four break points early in the fifth set, two at 1-1 and two at 2-2. Then, with Nadal serving at 3-2, Federer converted his second break point with a forehand winner after a 14-stroke rally that produced some of the best shots of the match.
It was Federer's first break since the second game of the match.
"If Rafael had won one of these, I think maybe now Rafael would be the champion," said Nadal's coach, Toni Nadal.
Nadal, who also lost to Federer in last year's final, had been trying to emulate another of Borg's records by winning consecutive titles at the French Open and Wimbledon.
Nadal, who played two other five-set matches in a tournament plagued by rain, was on the court for the seventh straight day.
After taking a 4-1 lead in the fourth set, the Spaniard called for a trainer to treat his right knee. Although he returned with tape below the knee cap, it didn't seem to slow him.
Federer finished with 24 aces, 65 winners and 34 unforced errors. Nadal had 50 winners and 24 unforced errors.
Nadal also used the "Hawk-Eye" replay technology, which is making its debut at Wimbledon, to great effect. One time, a call reversal in the fourth set infuriated Federer so much that he complained to the chair umpire after being broken for the fourth time.
"It's killing me today," Federer said after sitting down during the changeover.
In the first set, Federer converted his third break point in the second game, defensively returning a hard serve from Nadal and watching the Spaniard net a forehand.
In the tiebreaker, Federer jumped ahead 5-2 and thought he won the set on his third set point when leading 6-5, but Nadal challenged a call and "Hawk-Eye" showed his shot was in.
Federer wasted another set point at 7-6, but finally won with a backhand volley after Nadal sent a backhand into the net at 7-7.
Nadal broke Federer at 5-4 to win the second set, converting his first set point with a backhand winner. The Spaniard then pulled within two points of doing the same in the third set, coming back from 40-love to deuce. But Federer used a pair of volleys at the net to hold to 5-5.
Nadal was again two points from the set while leading 6-5, but after he put a forehand into the net, Federer served an ace and then finished it off with a service winner. Federer was broken again to open the fourth set, and Nadal added another to take a 3-0 lead.
The last Wimbledon men's final to go five sets was when Goran Ivanisevic beat Pat Rafter in 2001.