New Zealand's World Cup winning fly-half Stephen Donald looked to have stolen the headlines after entering the fray late on to make his Bath debut when he kicked a penalty to move the visitors two points clear with just a couple of minutes remaining.
But, after man of the match Duncan Weir had seen his last-ditch drop-goal attempt half charged down, Gray took advantage of a fortunate bounce to race on to the loose ball and go over for the match-winning score.
While tough on Bath, the Premiership outfit can have few complaints after failing to make use of their domination of territory and possession for long spells with seven penalties, six from the boot of Tom Heathcote, all they had to show for their efforts.
Glasgow managed two tries, Gray's late effort coming after Stuart Hogg had nipped over on the hour mark, while Weir contributed four penalties.
Bath were on the board first after just two minutes when Heathcote made Ryan Wilson pay for a high tackle on Dan Hipkiss, although Weir responded in kind just seconds later when Sam Vesty was penalised for using his hands at the ruck.
Heathcote edged Bath back ahead of 17 minutes with a fine strike when the hosts collapsed a scrum on halfway, but - somewhat against the run of play - it was Glasgow who went in ahead at the interval as Weir kicked two penalties in the space of five minutes with Bath twice pinged for failing to roll away at the ruck.
Weir had the chance to extend the lead just a couple of minutes into the second period as Bath were penalised for collapsing the scrum, but saw his kick drift narrowly wide and moments later, when Glasgow were pulled up for the same offence, Heathcote thumped over another long-range penalty to restore parity.
Bath moved back in front through Heathcote's fourth penalty, a superb effort from the touchline after Tommy Seymour offended at the ruck.
Glasgow responded in fine style with the game's opening try on the hour mark, teenage full back Hogg accepting an offload from replacement Troy Nathan and spotting a gap in the Bath defence to scamper over from close range. Weir added the extras to move his side four points clear.
The deficit was almost immediately reduced to just a solitary point when Heathcote kicked his fifth penalty after Ryan Grant was pulled up for stripping the ball illegally.
The 19-year-old fly-half then squeezed the visitors back ahead 12 minutes from time with his sixth successful kick, but that was to prove his last contribution as Bath supremo Sir Ian McGeechan opted to introduce Donald at ten.
But it was the New Zealander's opposite number Weir who made the next telling contribution, starting an attack with an incisive break and eventually kicking his fourth penalty after Anthony Perenise was caught using his hands at the ruck to make the score 19-18.
Donald appeared to have had the last say when he narrowly squeezed over a penalty after Ed Kalman had infringed, but he was trumped by Sale Sharks-bound Gray who stole the headlines in a dramatic finale as he charged past a wrong-footed Nick Abendanon to crash over.
Gilbert has released a new line of rugby cleats. The Gilbert Virtuo 8S is part of the exciting new product. Check it out.
The entire All Blacks apparel line has been updated for 2013/14. Check out the New Zealand All Blacks polo.
The Nike Tiempo is a solid rugby cleat and one of few styles still made from full-grain natural leather.
The Gilbert Blitz 8S rugby cleat is a great cleat at a great price of $69.99. Get a new pair of cleats today.
A cool looking all black rugby cleat with the high performance adidas is known for. Get in the Gear!
Wear the crest of the British and Irish Lions on your t-shirt. A great look for the summer.
The All Blacks Performance t-shirt is black with hints of blue from the training jersey. Very Cool.
The New Zealand All Blacks training jersey for 2013/14. Get in the Gear!
The USA Rugby Pro Alternate rugby jersey is perfect for any fan of the Eagles. Get yours to wear during the summer Test matches.
The NEW All Blacks 2013/14 jersey has arrived at World Rugby Shop. Dare to wear the colors of the All Blacks.