The Swiss fourth seed, who won the title in 2009, seemed to have the match in his grasp against the unpredictable Gulbis but faded badly to lose 6-7(5) 7-6(3) 6-2 4-6 6-3.
It was the fourth seed's earliest departure at Roland Garros since 2004 and the 2009 champion can have few complaints as Gulbis earned the biggest win of his career.
Federer made an unusually high number of unforced errors on the Philippe Chatrier show court and looked a subdued figure by the end of the match.
"I was a bit all over the place," admitted Federer. "I'm clearly very disappointed and have a lot of regrets now. It was a tough match and Gulbis did a good job to hang around and come back.
"On my side, I wished I could have played a bit better. I feel I should have done better from the baseline and I didn't really get into his service games."
Gulbis struck his groundstrokes with real venom and produced some deft touches to fully deserve his first place in a grand slam quarter-final since 2008, also in Paris.
A man who freely admits he has wasted his talent since that breakthrough at Roland Garros six years ago, world No.17 Gulbis said earlier in the week that he was "jumping on the last-chance train" at the age of 25.
He plays Tomas Berdych of Czech Republic in the last eight after the sixth seed beat American John Isner in straight sets.
"It was the biggest win of my career," said Gulbis who has already won two titles this year. "I've been playing very well in France, winning tournaments in Marseille and Nice and hope the next one will be here."
Gulbis went 4-2 up in the first set when a Federer backhand sailed long as the Swiss peppered the court with unforced errors. Federer broke straight back as a Gulbis forehand went wide and the two players upped the tempo in the tiebreak.
Gulbis went 5-3 ahead when Federer sent a routine smash wide but as the crowd chanted "Roger, Roger" the Swiss pulled back the deficit and a whipped crosscourt forehand won him the set.
His problems of consistency returned, however, in the first game of the second set when he was in command of a service game but allowed Gulbis to come back and eventually seal a break.
Federer broke back immediately, broke again for 5-3 and had two set points in the following game, both of which he wasted. On the first he tried a crowd-pleasing overhead but it was half-hit and Gulbis won the point before taking the game.
At 5-5 it was Gulbis's turn to waste two golden opportunities. On his second break point, the Latvian missed a simple drive down the line to win the point. He smashed his racquet on the court and stamped on it, breaking the frame.
He received a warning from the umpire and his 32-year-old opponent won the game with an ace.
In the tiebreak, Federer was leading 3-2 but a series of unforced errors led Gulbis to win five points in a row and take the second set.
Two poor shots by Federer, a backhand hooked wide and then a weak backhand into the next, gave Gulbis a 4-2 lead in the third set and he broke again to take the set on his first set point when Federer hit a forehand long.
The main question seemed to be whether the Latvian's volatile temperament would bubble to the surface as the finishing line loomed.
Federer's game was so off-key, he was going to need help to stay in the tournament and he received it in the fourth set when some Gulbis errors earned him a 5-2 lead.
The 25-year-old Gulbis was granted a medical treatment break and when he returned he appeared rejuvenated, breaking Federer's service immediately for 5-3, on the way to winning 10 of the next 11 points.
He had chances in the following Swiss service game too but Federer, living close to the edge, hung on to win the set on his first set point.
Gulbis broke early in the fifth set as Federer's form suffered another slump and it went with service until Gulbis served for the match.
An ace took Gulbis to match point and Federer hit wide on the backhand to give the Latvian victory.
Watch all the action from the French Open on up to court seven on the Eurosport Player
Results from the French Open Men's Singles matches on Sunday
2-Novak Djokovic (Serbia) beat 13-Jo-Wilfried Tsonga (France) 6-1 6-4
18-Ernests Gulbis (Latvia) beat 4-Roger Federer (Switzerland) 6-7(5) 7-6(3) 6-2 4-6 6-3
6-Tomas Berdych (Czech Republic) beat 10-John Isner (U.S.) 6-4 6-4 6-4
8-Milos Raonic (Canada) beat Marcel Granollers (Spain) 6-3 6-3 6-3
7-Andy Murray (Britain) beat 28-Philipp Kohlschreiber (Germany) 3-6 6-3 6-3 4-6 12-10
24-Fernando Verdasco (Spain) beat 12-Richard Gasquet (France) 6-3 6-2 6-3
Watch action from the French Open up to court seven on the Eurosport Player
- Sports & Recreation