Some questions have been going around: what happens if two parties tie in the seat counts? What happens if the prime minister's party doesn't finish first in a minority parliament? What really happens if the prime minister can't pass a throne speech?
There's a lot of misconceptions out there, propagated by the party leaders themselves, but the reality is that the prime minister remains the prime minister after an election until he either resigns, or is fired by the Governor General for failing to gain the confidence of the House of Commons (and not subsequently resigning). This is actually true whether or not you win a majority mandate, come first in a minority parliament, or lose an election outright and have another party win a majority of seats. Kim Campbell was the prime minister of Canada not until the results of the 1993 election came in, but until she resigned. She could have decided not to resign, and called the House together, and faced the inevitable result of being turfed out on her ear. She chose not to.
So, in a minority parliament, it doesn't matter which party gets the most seats: the prime minister has the first crack at trying to gather enough support to pass a Speech from the Throne. If he gets that support, he gets to continue to be prime minister. Both Stephen Harper and Justin Trudeau are wrong on this point. Mind you, the prime minister can choose to resign if he or she doesn't win a plurality of seats, and if this is what Harper promised, and if this is what Harper does in such an event, then tradition has still been fulfilled.
If the prime minister is unable to gather support to pass a Speech from the Throne, the Governor General has the option to turn to the leader of next party and ask him or her to try and pass a Speech from the Throne. If he gets it, then that leader becomes the next prime minister. If not, then the Governor General looks for other candidates (possibly a coalition of MPs from both parties that have broken away from their various leaders). This goes on indefinitely until somebody can gather the confidence of the House, or it's clear that no one can, at which point parliament is dissolved and elections are called.
The point is, we don't elect prime ministers. We elect members of parliament who campaign on their promise to support a particular leader for prime minister but, more importantly, act as our representative in the House of Commons. In a minority parliament -- and even in some cases, majority parliaments -- that promise to act as a representative can (and should, depending on the circumstance) trump party loyalty. So, interesting things can theoretically happen if party discipline breaks down.
Look at what happened during the 1971 general election in Newfoundland. Premier Joey Smallwood had led Newfoundland since its entry into confederation in 1949, and was increasingly criticized for his autocratic approach to governing. After a raucous leadership campaign in 1969, where he resigned, and then entered the subsequent leadership race when he saw that his chosen successor was losing to John Crosbie, his party went into the election deeply divided. The final result of the October 28 election gave the Progressive Conservatives, led by Frank Moores and (a probably quite exasperated) John Crosbie 21 seats, while the Liberals took 20. The remaining seat was held by the lone MLA for the Labrador Party, Tom Burgess, which meant a minority legislature.
Moores had won the election, taking a plurality of seats and, incidentally, 51.3% of the popular vote, but Smallwood didn't resign as premier immediately. Smallwood's Liberals initially contested several recounts but, on November 11, Smallwood announced he would resign as premier and as leader of the Newfoundland Liberals, calling a leadership convention to be held in February. Burgess, a former Liberal MLA who quit the party after being passed over for a cabinet position in Smallwood's government, threw his support behind Moores, theoretically giving power over to the Conservatives.
But this result was conditional on the result of six recounts. Five found that the results of the night still stood, while the sixth couldn't be completed because ballots at a polling station were burned soon after being counted on election night. The case went to the Supreme Court of Newfoundland which decided that the election night results would stand. Joey Smallwood called a press conference on January 13, 1972 to announce he would resign as premier and vacate the office -- three months after the election that had defeated him.
However, the story doesn't end there. Two days later, Labrador Party MLA Tom Burgess announced that he was revoking his support for the Conservatives, citing a broken promise from premier-designate Frank Moores (Burgess had been passed over for cabinet, again). Wikipedia claims, without attribution, that Smallwood had enticed Burgess to the Liberal side, promising that Burgess would succeed him as Liberal leader and premier of Newfoundland. Smallwood also managed to entice a Conservative MLA, Hugh Shea, to cross the floor, switching the seat counts in the legislature to 21 Liberals and 20 Conservatives.
Well, the leadership convention was held on the weekend of February 4-5, 1972. Tom Burgess did not win it (lord knows how he felt), and instead Ed Roberts wins as Liberal leader, and he immediately begins pressuring premier Moores to call the House to session so the Liberals could take back power at the earliest opportunity (it's worth noting that the Newfoundland legislature has not sat since the October 1971 election). This was complicated, however, by the resignation of Liberal MLA W. Augustus Oldford, and a by-election had to be held. Even though the Liberals win that by-election on February 28, another Liberal MLA announces that he will not take his seat. Eventually, the Conservatives call the Newfoundland legislature to order on March 1, 1972. With a Conservative speaker, the Conservative government of 19 MLAs faces an opposition of 20 Liberals.
Given all these shenanigans, Moores quite reasonably goes to the lieutenant governor and tells him the legislature is unworkable, and could he please dissolve it and call an election. The election that followed had thoroughly exasperated Newfoundlanders award the Conservatives with 33 seats to the Liberals 9, rendering the whole matter moot.
Smallwood wasn't done, amazingly. It seems the Liberal party of Newfoundland started to tear itself apart after its loss and when Smallwood failed to regain the leadership at a convention in 1974 (I'll bet members ofhis party was thoroughly sick of him by this point), he broke with the Liberals and formed his own, calling it the Newfoundland Reform Liberal Party. He ran 28 candidates in the 1975 provincial election, took 12% of the vote and won four seats, splitting enough of the vote with the remaining Liberals to give Frank Moores his second majority mandate. Smallwood wouldn't quite politics altogether until 1979.
Whatever complaints we have about politicians today, Smallwood sets the gold standard of Not Letting Go.