The first rule is that when we hold an election we do not directly elect a prime minister. We elect a House of Commons. It is this elected chamber of Parliament that decides who governs the country.
The second rule of parliamentary government is that it is the leaders of the party or coalition of parties that have the confidence of a majority in the House of Commons who have the right to govern.
The rest of the details, in all glorious accuracy here.