“It is my longest and hardest race of the year,” Cancellera, a native of Bern, said before the championship race, according to Swiss media.
The grueling 272-kilometre competition was held in driving rain on roads slippy with wet leaves that led many riders to fall, including Swiss competitors Martin Elmiger and Oliver Zaugg.
The entire British side, including stars Bradley Wiggins and Chris Froome, failed to finish.
Cancellara avoided falling but could not keep up in the final stages with the climbers.
Costa finished ahead of Spaniards Joaquin Rodriguez and Alejandro Valverde to become the first Portuguese winner of the coveted rainbow jersey.
The 26-year-old Portuguese caught Rodriguez with less than 200 metres to go.
"This has been a big dream of mine for a long time. I still can't believe I'm the world champion," said Rui Costa, who broke down in tears on the podium.
"Two kilometres from the finish Rodriguez held a certain advantage but I still had to choose the right moment to make the jump and catch him, and then to try and outsprint him."
Costa, who won two stages at July's Tour de France as well as the Tour of Switzerland, succeeds Philippe Gilbert of Belgium as world champion. Gilbert finished ninth, 34 seconds behind, and just ahead of Cancellara.