Bayern are still licking their wounds after being dumped from the Champions League last Tuesday when holders Inter Milan earned a 3-2 win in Munich to reach the quarter-finals and Saturday's 2-1 win at Freiburg boosted moral.
With his side also out of the German cup and lagging 14 points behind league leaders Dortmund, Bayern face the prospect of a season without silverware.
Their aim now is second in the Bundesliga, to earn a place in next season's Champions League, and Van Gaal, who leaves at the end of the season, was glad to get back to winning ways.
"We were lucky not to fall behind in the first half," said the Dutchman, whose side is fourth in the league.
"We didn’t play well. We were a different team in the second half and at the end of the day, we've won a little luckily thanks to Franck Ribery's goal."
Germany striker Mario Gomez, the league's top scorer, put Bayern ahead with his 19th league goal, his sixth from a header, after just nine minutes.
Bayern goalkeeper Thomas Kraft saved a penalty from Papiss Demba Cisse after 14 minutes, but Cisse was not to be denied his 18th league goal of the season when the Senegal striker equalised on 17 minutes.
Ribery sealed the win when he was sent clear by Ukrainian Anatoliy Tymoshchuk and fired home on 88 minutes.
"We knew it would be difficult after the heavy blow on Tuesday," said captain Philipp Lahm.
"We played well in the second half and we ultimately deserved to win.
"You don’t have to play stunning football every time.
"We need wins, we need three points every time, and if so, we'll make it into the Champions League.
"We’ve taken three points away from home, and that's what matters."
On Saturday night, Mainz poached a point at Bundesliga leaders Borussia Dortmund as Croatia striker Petar Sliskovic's controversial late goal sealed a 1-1 draw.
Despite the equaliser, Dortmund remain 10 points clear at the top of the table, but second-placed Leverkusen can trim their lead to seven points if they beat Schalke 04 - Dortmund's deadly rivals - at home on Sunday.
Dortmund took the lead when Germany defender Mats Hummels managed to scramble the ball over the line after just eight minutes.
Then Turkey midfielder Nuri Sahin had his weak penalty attempt saved after 19 minutes which proved costly in the dying stages.
With Dortmund defender Neven Subotic laid out on the pitch after taking a ball in the most delicate of areas between the legs, Mainz scored as Sliskovic fired home with time almost up.
"It is out of the question that my players saw the injured player and knowingly played on regardless," said Mainz coach Thomas Tuchel in defence of his side.
Dortmund coach Jurgen Klopp was left fuming on the touchlines as the referee had waved on play with Subotic still laid out on the turf.
"You had the opportunity, which you didn't take, to put the ball out," fumed Klopp in reply to Tuchel.
"You saw Subotic laid out, our bench saw everything out there, but you didn't give a damn!
"I was especially unhappy about the celebrations."
"It shouldn't be like that in football.
"Having said that, the result is fair because Mainz were a strong opponent, things didn't go our way and we missed an early penalty."