Prime Minister Justin Trudeau of Canada was headed to a second term on Monday after an often ugly campaign that became a referendum on his character and on his authenticity as an earnest standard-bearer for liberalism.

Mr. Trudeau’s Liberal Party will not retain its majority in Canada’s House of Commons, according to projections by the Canadian Broadcasting Corporation, but it will keep enough seats to allow Mr. Trudeau to form a government, with support from two left-leaning parties.

The victory was a personal vindication for Mr. Trudeau, who battled accusations that he had bullied his former attorney general, an Indigenous woman, and faced damaging revelations late in the race that he had dressed in blackface and brownface as a young man.

But these problems also clearly took a toll. The Liberals’ share of the popular vote fell throughout the country as it was losing its hold over the House of Commons.