It was a game that really lacked any quality at all but Costa, who had a poor game up to the point he scored, hit a wonderful left-footed strike from a long way out in the 71st minute that thundered into the top corner of the net.
It left the hosts stunned as they deserved at least a point from the game but it wasn't to be and now they stay bottom of the table and in a real scrap in order to stay up this season.
Hosts started the game the better side
Bayern looked nothing like their usual selves during the first half against their struggling hosts, who made a fast start to the game.
Straight from kick off they were in the faces of Bayern and almost scored within the first minute of the game but Thiago made a crucial last-ditch block to save the visitors from going behind.
The hosts continued on though and they created another good chance of the game within the first five minutes of the game when Jan Rosenthal rose high in the box from a corner kick but he put his header wide of the goal when he should have hit the target.
After the early chances though the hosts struggled to create anything else but were very solid in their defensive shape against the visitors, who had lots of the ball without creating loads of chances
Their best chance of the half came from a free kick from Costa which found Mats Hummels unmarked in the box but under pressure, the defender put his header over the bar.
Overall though the defending champions, who knew that this was a must win the game in order to go back to the top of the table, were very poor during the half and knew they had to improve in the second half to get the win.
Visitors stepped things up at the beginning of the second half
Whatever Carlo Ancelotti said at half time seemed to take effect as it was noticeable straight away that Bayern started to play with more intensity.
They almost went in front when Thomas Müller was played in on goal down the right hand side but he saw his goalbound effort well saved by Michael Esser.
Costa's stunner ensures three points for Bayern
Even though they created this early chance in the second half, they really struggled to break down a stubborn home defence until a moment of magic saw them go in front.
Thiago played the ball into the path of Costa after a quick free kick and the winger didn't think twice as he thundered a stunning left-footed strike from a long way out which went flying into the top corner of the net, leaving Esser with no chance of keeping it out.
The goal stunned the hosts for the next ten minutes but they almost managed to get an equaliser just before the end when Jerome Gondorf's free-kick was well-saved by Manuel Neuer.
Apart from this chance though, Bayern easily saw the game to a close to get the job done without playing at their best in a game that they knew they had to win.