The prime minister is typically the leader of the largest party in the Commons. Since 1963, by convention, the prime minister is a member of the House of Commons.
Prime ministers must be an MP or Lord. The handful appointed to the post who were outside parliament subsequently entered, either by a Commons by-election or by receiving a peerage.
The prime minister picks a cabinet from Parliament colleagues with functions as the PM sees fit. Unlike the US, the task areas assigned cabinet ministers do not necessarily have a strict one-to-one correspondence to executive bureaucracies. There is considerable flexibility.