Neither side could score during the first five rounds and Peter Budaj stopped Rich Peverley to begin the sixth. Gallagher then slid a shot through the pads of Tuukka Rask to bring the Canadiens their first win in three tries.
Boston scored four times in the second period and took a 4-2 lead into the third. Brad Marchand's 14th goal of the season was part of the Bruins' second-period offense.
Tyler Seguin scored on a backhander with 8:10 to play in regulation to put the Bruins in front 5-3, but Gallagher tallied 28 seconds later and Andrei Markov forced overtime with just 9 seconds remaining when he produced a power-play goal on a slap shot.
Montreal moved a point in front of Boston for the lead in the Northeast Division.
Carey Price opened in goal for Montreal and allowed four goals on 26 shots before being replaced by Budaj. Rask gave up five goals on 28 shots.