Toro Rosso's Sebastian Vettel took the final points place, holding off Heikki Kovalainen's McLaren in the closing laps.
"I never struggled so much, I was pushing so much to do it and I managed to do it so it was a great race," said Kubica.
"The last laps were very, very difficult. The team was telling me to stay calm and not push too much but I wasn't pushing for 10 or 15 laps, it was just amazing.
"It's very fantastic to win my first race for BMW Sauber, which I joined from the beginning. We grew up together."
Hamilton looked to have the race in the bag until the intervention by the safety car.
The Englishman had built up a 6.8-second lead over Kubica when the safety car was deployed on lap 17 so that marshals could remove the stranded Force India of Adrian Sutil.
All the leading drivers came in for fuel and tyre stops as soon as the pit lane was open on lap 19.
But as they finished their stops, the pit-lane exit was still closed.
Raikkonen, who had entered the pits in third place, and Kubica stopped side by side at the red light, but Hamilton failed to notice in time that they had stopped, and he ploughed into the back of the Ferrari.
The incident broke the right-front suspension of Hamilton's car, putting him out of the race, and damaged the rear of Raikkonen's Ferrari sufficiently for him to be forced into retirement, too.
"I don't know really what happened," Hamilton said.
"I was comfortably in the lead. It was looking like it was going to be an easy win. We came in. It wasn't a great stop.
Hamilton looked on course for a win when he made his error
"I saw the guys in front of me, and all of sudden they'd stopped. I saw the red light but, by the time I saw it, it was too late to stop.
"I apologise to Kimi if I took him out, but that sort of thing happens sometimes."
Hamilton said he had been told by the team that the red light was on at the end of the pit lane, but that the warning came "a little bit late".
The incident cost Hamilton the championship lead, which has now been assumed by Kubica.
The BMW driver, who has driven an impressively consistent season to deliver on promise that has been evident since he made his debut in 2006, is four points clear of Hamilton, who is tied on points with Massa.
Massa's race was ruined when he had to stop twice within a lap because of a breakdown in communication during the safety car period.
He queued behind Raikkonen, only to find that Ferrari could not refuel him, and he had to do another lap.
When he did come in, Ferrari chose to keep him on a two-stop strategy - like Kubica - but the ground he had already lost ensured he was out of contention for a top finish.
Hamilton's exit threw the race wide open, but Kubica always had the upper hand.
Heidfeld led as the safety car pulled off, ahead of eight other cars which did not stop during the caution period, and Kubica in 10th place.
Heidfeld quickly pulled out a huge lead over Barrichello's Honda in second place, and was able to rejoin still ahead of Kubica and fuelled to the end of the race when he made his one and only pit stop on lap 29.
But within two laps Kubica passed him into the first corner in what looked like a co-ordinated piece of team play as BMW Sauber tried to maximise their chances.
Kubica, hailed as a future world champion since he replaced Jacques Villeneuve at BMW Sauber mid-way through 2006, set about building enough of an advantage to enable him to make a second stop without losing the lead.
Heidfeld was left to fend off Fernando Alonso's Renault, which had run fifth from the start and, like Kubica, also needed to stop again.
The German did his job to perfection, and Alonso eventually spun coming out of turn seven on lap 46, possibly because of a glitch in his gearbox.
Kubica lost a lot of time behind Timo Glock's Toyota between laps 38 and 42, but he was more than able to pull out the advantage he needed in the seven laps before his final stop.
His win comes at the circuit where he suffered a huge accident only one year ago.
Results from the Canadian Grand Prix, Montreal
1. Robert Kubica (Pol) BMW Sauber 1:36:24.447 2. Nick Heidfeld (Ger) BMW Sauber 16.495 secs behind 3. David Coulthard (GB) Red Bull-Renault 23.352 secs 4. Timo Glock (Ger) Toyota +42.627 5. Felipe Massa (Brz) Ferrari +43.934 6. Jarno Trulli (Ita) Toyota +47.775 7. Rubens Barrichello (Brz) Honda +53.597 8. Sebastian Vettel (Ger) Toro Rosso-Ferrari +54.120 9. Heikki Kovalainen (Fin) McLaren-Mercedes +54.433 10. Nico Rosberg (Ger) Williams +57.749 11. Jenson Button (GB) Honda +1:07.540 12. Mark Webber (Aus) Red Bull-Renault +1:11.229 13. Sebastien Bourdais (Fra) Toro Rosso-Ferrari 1 lap behind R Giancarlo Fisichella (Ita) Force India-Ferrari 51 laps completed R Kazuki Nakajima (Jpn) Williams 46 laps R Fernando Alonso (Spa) Renault 44 laps R Nelson Piquet (Brz) Renault 39 laps R Lewis Hamilton (GB) McLaren-Mercedes 19 laps R Kimi Raikkonen (Fin) Ferrari 19 laps R Adrian Sutil (Ger) Force India-Ferrari 13 laps
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.