In general, when Canadians vote they expect that whichever party wins the most seats will become the government and that their leader will be the new Prime Minister. However, Canada's coalition government rule could change that. Under this rule, Justin Trudeau could still be the prime minister even if he loses the election by forming a coalition government.
Unlike other countries, in Canada we don't actually elect our Prime Minister, we just elect our government. That means that we vote for MPs who are members of a specific party. Whichever party has the most MPs or seats out of the 338 available wins the election. The leader of that party then becomes the Prime Minister and is appointed to the role by the Governor General.
However, if the party wins but holds less than half of the 338 seats, they only have a minority government. In this case, the parties that come in second and third could form a coalition, by joining all their MPs together and they could form the government. If that coalition were to involve the Liberal Party, Justin Trudeau could remain the Prime Minister.
That's because on election night, even if the Liberals don't get the majority of the seats, Trudeau doesn't have to concede government. CBC reports that "the incumbent prime minister remains prime minister until he or she formally resigns or is dismissed by the Governor General."
In the case of a Conservative minority government, if the majority of the MPs in the house support Justin Trudeau as their leader, he can continue to govern. He would have until mid-January to make it happen.
While it's unclear right now whether that will happen in the election, aggregate polls from CBC, which combine all of Canada's publicly available polling data (including Ipsos, Nanos, Mainstreet, and more) suggest a minority government is likely. In fact, as of October 17, these polls suggested there is a 44% chance the Conservatives will win a minority and also a 44% that the Liberals will win a minority.
That means a coalition government is not off the table, especially as NDP leader Jagmeet Singh has said he is open to this kind of deal before, but not with the Conservatives.
The Conservative Party, however, says they are confident that this kind of coalition will not be formed. In an interview with CTV News, Andrew Scheer said that "we would expect that other parties would respect the fact that whichever party wins the most seats gets to form the government and that they will understand that if Canadians — when Canadians endorse our platform, we will have the right to implement it"
On his Twitter, he told a slightly different story though. In a video shared earlier this week, Scheer said, "I just found out that Justin Trudeau is planning on forming a coalition government with the NDP so he can remain prime minister."
He went on to say that the two parties were "ganging up together" and attacked the coalition saying it's "the coalition you can't afford."
In Canadian history, there has only ever been one federal coalition government, referred to as the Great Coalition of 1864. That coalition eventually led to Confederation.
The second time a coalition almost occurred was more recently, in 2008. After Stephen Harper won the election, the Liberals, NDP, and Bloc Quebecois agreed to form a coalition together; however, it never actually happened.
Whether it could happen again is yet to be seen after Canadians take to the polls on October 21.