The Blues booked their place despite playing two-thirds of the match with 14 men after winger Tom James was red carded after 28 minutes for a head butt.
Gloucester now face the difficult task of having to score maxiumumpoints at Biarritz next week and hope other results go their way if they are to qualify.
The Cherry and Whites looked to have held off the Blues on a boggy pitch but a try in the dying minutes from replacement forward Bradley Davies put the visitors in front for the first time to earn David Young’s side a remarkable triumph.
Olly Barkley opened the scoring with a penalty from inside his own half which was answered by Leigh Halfpenny but Gloucester hit back immediately through Barkley’s second penalty.
James and Olivier Azam then had an altercation with the Frenchman hitting the deck after the Blues winger appeared to put his forehead into the hooker’s face. Referee Alan Lewis had no hesitation in sending James off. With the Blues down to 14 men, Barkley slotted his third penalty to give the home side a 9-3 lead at the interval.
Halfpenny clawed back three points in the opening exchanges of the second spell. Despite being a man down, the blues took the game to Gloucester with backrowers Martyn Williams, Maam Molitika and Andy Powell causing a lot of damage with telling runs.
Barkley kicked his fourth penalty to edge Gloucester further ahead but Halfpenny responded with eight minutes remaining to bring the Blues within three points.
With the Blues throwing the kitchen sink at Gloucester in the dying moments, the visitors stunned the home fans after setting up a couple of rucks close to the try-line before Davies picked up from a metre out and crashed his way over. Halfpenny’s conversion from wide out added the icing to the cake for the Blues and left Gloucester in a state of shock.