You have to understand that so much in the British system is based purely on convention and tradition and has no official rules set in stone.
The PM isn't even an official position and isn't elected by the House. The only House elected positions are the Speaker and his/her deputees, with the conventional caveat that the Speaker comes from the party with the second largest number of seats (though this is periodically not the case).
The sitting PM (usually the leader of the party with the most seats at the last election) is invited to chat with the Monarch.
He or she offers their resignation as PM and recommends the next PM (conventionally the leader with the most seats in the most recent election).
The Monarch chooses to decide as to which member of the Commons is to be his/her Prime Minister after hearing this advice.
In practice the leader of the party with the most seats becomes the Prime Minister.
It becomes a bit more wobbly during a coalition as the sitting Prime Minister may negotiate with a smaller party and deny the marginally more successful (in terms of seats) party leader to remain in power by dint of being the leader with the most seats during the coalition.
If the party of with the largest number of seats removes their leader or the leader resigns during a Parliament the current PM will visit the Sovereign and recommend the next Prime Minister, much like the morning after an election. Usually the new leader of the party then becomes the Prime Minister. The convention in this case is the Chancellor becomes the new leader of the party and therefore the new PM (e.g John Major, Gordon Brown etc).
The departing PM is then offered membership of the highest order of chivalry in their country (The Order of the Garter, The Order of the Thistle etc) and a Peerage (seat in the House of Lords) either or both of which they can decline.
I'm still not sure what happens in the case of aliens killing the sitting Prime Minister or the PM being shot by his.her spouse after a year of world domination that is undone by reversing time.