James Lang's penalty with four seconds remaining sealed victory after Ross and James Chisholm crossed for tries to help level the scores 27-27 following Richard Hibbard's sin-binning.
Gloucester scored 17 unanswered second-half points, fighting back from 15-10 down to build the game's biggest lead.
Hibbard's yellow, however, was costly.
Moments after the hooker was sin-binned for a penalty at the scrum, James Chisholm went over to give the Londoners renewed hope of taking something from the game against their fellow top-six hopefuls.
A slick passing move, which involved Marland Yarde and Alofa Alofa, put the older of the Chisholm brothers, Ross, over in the 75th minute.
And while 21-year-old fly-half Lang was off target with his conversion attempt, a penalty at the scrum with moments remaining allowed him a shot from right in front to win it.
It was an enthralling finish to a match which started with Charlie Sharples going over for the hosts, only for Gloucester to be punished for failing to make more of their strong start, as Tim Visser touched down on his return from Scotland duty and Charlie Mulchrone helped edge Quins ahead before the break.
Billy Burns landed his third kick of the game to convert David Halaifonua's try and restore the hosts' lead after half-time, with Sharples grabbing his second try of the game as Gloucester quickly built a comfortable lead.
The one-man advantage was ruthlessly exploited late on as Quins move to within two points of Northampton in sixth, while a losing bonus point leaves Gloucester ninth in the table and eight points adrift of a European Champions Cup spot.
Gloucester director of rugby David Humphreys:
"It's a sense of frustration, anger and disappointment after those last six or seven minutes of madness.
"We led by two scores and had to get the basics right but our defence and set-piece went to pieces after we had controlled the second half.
"European qualification is still mathematically possible but we had to get four or five points today and we didn't so it's a massive blow for the squad, especially after a great win over Saracens in our last home game."
Harlequins director of rugby John Kingston:
"At the end of the first half we led 15-10, but we were much the better side and should have been further in front. They then had a good period but we came back strongly and deserved to win.
"We knew we had to control the environment today as the influence of the crowd is very important here. I thought we managed to do so, for which the leaders in our team must take great credit.
"We've kept our European Cup hopes alive as finishing sixth or maybe seventh will be enough to secure us a spot for next season."
Gloucester: Marshall; Sharples, Scott, Twelvetrees, Halaifonua; Burns, Heinz (capt.); Thomas. Hibbard, Afoa, Galarza, Thrush, Ludlow, Rowan, Morgan.
Replacements: Matu'u, Hohneck, Doran-Jones, Savage, Kalamafoni, Braley, Hook, Trinder.
Harlequins: R. Chisholm; Yarde, Marchant, Hopper, Visser; Swiel, Mulchrone; Lambert, Gray, Sinckler, Merrick, Matthews, J. Chisholm, Ward (capt.), Luamanu.
Replacements: Buchanan, Owens, Shields, South, Naoupu, Jones, Lang, Alofa.
For the latest rugby union news follow @bbcrugbyunion on Twitter.
Harlequins scored 15 points in the final eight minutes against 14-man Gloucester to clinch a dramatic victory at Kingsholm.