The visitors appeared in a comfortable position after Toby Flood’s converted try increased their lead to 11 points at the beginning of the second half.
But Montpellier came roaring back and two tries from Lucas Dupont kick-started their recovery before they sealed victory by the smallest of margins to give them their only win in the group.
It was Montpellier who started well as two penalties from Ben Lucas and one from Benoit Paillaugue put them 9-0 ahead just after the half-hour.
Clement Poitrenaud’s converted try narrowed the gap to two points before Toulouse claimed the lead for the first time thanks to Luke McAlister’s penalty.
The New Zealand fly-half added another three points shortly before the hooter while it appeared the visitors were well on their way to victory when Flood crossed the whitewash three minutes after the resumption.
But Dupont touched down twice in the space of five minutes soon after, first reducing arrears with a converted try in the 46th minute before his next score gave Montpellier a one-point lead.
Flood put Toulouse back in front with a penalty and did so again after Paillaugue had found the posts.
But it would be the hosts who had the last laugh as Ben Lucas slotted home his third penalty in the 68th minute – and they held out for an unlikely victory.
Toulouse’s slip-up meant Bath finished top of the pool following their 20-15 victory over Glasgow at The Rec.
Two second-half penalty tries underpinned a Bath win that meant they finished top of Pool Four. It is the first time in European Cup history that a team has lost its first two group games – Bath were beaten by Glasgow and Toulouse – and still reached the last-eight.
But they were made to sweat and toil by a resilient Glasgow outfit, who stunned Bath with an early Alex Dunbar try and a brilliant Richie Vernon touchdown, while Finn Russell kicked a conversion and penalty.
George Ford booted two penalties and a conversion for Bath, with Ollie Devoto adding a conversion, ensuring that the hosts progressed after a nerve-shredding finale.
BATH: Watson, Agulla, Joseph, Eastmond, Banahan, Ford, Cook, James, Webber, Wilson, Hooper, Day, Garvey, Louw, Houston.
Replacements: Burgess for Ford (65), Stringer for Cook (62), Auterac for James (49), Batty for Webber (49), Thomas for Wilson (49), Attwood for Day (55), Fearns for Houston (62).
GLASGOW: Maitland, Seymour, Bennett, Dunbar, van der Merwe, Russell, Pyrgos, Reid, MacArthur, Cusack, Gray, Kellock, Nakarawa, Brown, Vernon.
Replacements: Horne for Dunbar (45), Matawalu for Pyrgos (69), Yanuyanutawa for Reid (41), Welsh for Cusack (41), Eddie for Kellock (62).
Referee: John Lacey (Ireland).