The 25-year-old has been at Dortmund since the 2010/11 season, but his current contract was due to end in June.
"We're delighted to have sealed this transfer and that both parties have signed a contract until 2019," said Bayern chairman Karl-Heinz Rummenigge, describing Lewandowski as "one of the top strikers in the world."
"He's going to strengthen Bayern," added Rummenigge.
Lewandowski had dropped a heavy hint that he planned to join Bayern Munich by responding in Bavarian to a question about his future two weeks ago.
"Schaun mer mal ('let's wait and see' in Bavarian)," Lewandowski told German daily Bild when asked about a possible move to Munich.
His 11 goals this season have helped his team to fourth place in the Bundesliga, but they are 12 points behind runaway leaders Bayern.
The Polish international was also a key player in the Dortmund's European campaign last season, scoring four goals against Real Madrid in the UEFA Champions League semi-finals before falling to Bayern in the final in Wembley.