The closing agent (escrow) will divvy up the proceeds from the sale. Of which, if there is a mortgage, that bank will get what is needed to pay the loan in full, the agents commissions, any service providers that are being paid from closing proceeds, as well as the seller to get whats left over.