US Open champion Thiem looked to be sailing through to the last eight when he led Gaston, ranked 239, by two sets to love but the 20-year-old Frenchman battled back to force a decider.
Thiem, who had not dropped a set in his first three matches, began to look both physically and mentally weary as Gaston, who defeated Stan Wawrinka in the third round, dragged him to the net time and again with drop shots.
But Thiem held his nerve in the fifth set as his opponent showed his inexperience at crucial moments, the Austrian eventually winning 6-4 6-4 5-7 3-6 6-3 after three hours and 32 minutes.
The 27-year-old said: “I think it was an amazing match for both of us, such good fighting qualities for him. I haven’t seen for a very long time a player with such a big touch in his hands. His drop shots were from another planet.
“I was losing a little bit of energy, he was playing amazing. In the fifth set I found somehow new energy. It was a great fight to the end and I’m very lucky I made it through today.”
Gaston had mixed emotions after the match, the 20-year-old saying: “The crowd was just amazing. I love to play in Paris. Today of course I’m sad, but I’m also happy.”
In the last eight, Thiem will face 12th seed Diego Schwartzman, who has been in great form in Paris and beat Lorenzo Sonego 6-1 6-3 6-4.
The third seed admitted he is feeling the pace physically, saying: “I’m happy of course to face one of my best friends from the tour in the quarter-finals. From my perspective, it’s all about recovery.
“Obviously the last weeks were tough. I’m not running on a full tank any more. That’s for sure. So I try to recover as good as I can. If I’m able to do that, it’s going to be an exciting match.
“If not, I think he’s going to be the heavy favourite. But I’m trying to do everything to have a great recovery, to have a good day off tomorrow, and then go out on Tuesday as fit as possible.”