Shea Weber scored the winning goal on the power play with just under seven minutes left in the third period of the quarter-final game.
The defending champions will play the United States in the semifinals in a rematch of the gold-medal game four years ago in Vancouver.
The loss tarnished an outstanding performance by 21-year-old Latvian goalie Kristers Gudlevskis, who was brilliant as Canada outshot the Latvians 57-16.
But Gudlevskis couldn't find Weber's blast from the point with 6:52 remaining that gave Canada some much-needed breathing room.
The Canadians narrowly missed going ahead just before the midway point of the third but video replay showed the puck didn't entirely get over the goal-line.
Patrick Sharp had the other goal for Canada.
Lauris Darzins scored for Latvia.
The U.S. beat the Czechs 5-2 in their quarter-final to advance. Earlier, Sweden and Finland advanced to a semifinal showdown with quarter-final wins. The Swedes downed Slovakia 5-0 while the Finns shocked host Russia 3-1.
Also on HuffPost