Assume you have a 100k mortgage and have paid off 15k of the principal. If you sell the house for 100k do you get 15k and the bank gets 85k, or does the bank get all of it if/when it sells until you own 100% of the principal, then you get 100% of it?
What if you own 15k on a 100k mortgage and it sells for 150k, do you get 15k or 22.5k?
You get 100k, but you have to pay off the 85k balance on your mortgage, leaving you with a net of 15k. You can’t carry a mortage on a house you no longer own.
If it sells for 150k, you get 150k, then you have to pay off the 85k balance, leaving you with 65k.
My guess is that the institutions don’t let you touch the money until they get their share. Since a buyer will rarely have $100k underneath a mattress, and banks have an interest in making sure they get paid, the buyer bank will probably transfer the funds directly to your mortgage bank. The mortgage bank will then take what is owed, and cut you a check for the remainder. Other middlemen (realtor, title agency, etc.) will also have figured out ways to get their cut before the money gets into your hands.
Of course, the buyer will not instruct his bank to pay your bank until all the paperwork is signed and the deal is closed.
I just re-read the OP, and I think I missed the question.
A mortgage is simply the bank is giving you a loan for $100k, with rights to take your home if you default. The bank does not care whether the value of your house increases or decreases. They will, regardless, get paid their $100k plus interest.
If you discover oil on your property, and the value of your house increases 500x, the bank does not get a windfall. You simply pay them their $100k, and that is that. The amount of equity you have in your house (equal to “current value” - “remaining loan amount”) is of no concern to the bank (unless you want to refinance).
At least in the US, this is almost always handled by the escrow company. They’re a disinterested party that everyone trusts. So seller, seller’s bank, buyer, buyer’s bank, termite inspector, city tax agency, everyone, gives their money or their bill for payment to the escrow company.
And “closing” is the process of everyone signing all the papers and the escrow company divvying up the loot to the right parties. Nobody gets paid until everyone has thrown in their money, and everyone has signed agreeing on the divvy.
It doesn’t necessarily all happen at once, but having an escrow company in the middle makes sure the various parties don’t get into a situation where “I won’t pay until he pays and he won’t pay until I pay”.
And the escrow company charges a couple hundred bucks for their trouble, holding all the cash & paperwork.
When you prepare to sell your home, your lawyer will request a “payoff letter” from the mortgage bank, the figure to be calculated “as of” a specific date. The letter will also include a “per diem” amount to add or subtract to the amount if the closing happens earlier or later.
In short, the request to the lender says “I will be selling my house on April 30. How much will it cost me to settle my loan with you on that date (if all previously scheduled payments have been made on time) and how much is the daily interest in case I sell it a few days before or after.”
The figure usually gets fudged a little bit in favor of the bank. After the closing everything is recalculated, including all taxes, interest and fees. Normally, the seller gets a check a month or two later from the bank for the variance. It might just be a couple of hundred dollars. The same thing happens in a refinancing.
The only fly in the ointment is if there is a pre-payment penalty for paying your mortgage off before a certain date. Then you can get creamed for a substantial fee, maybe between 1% and 5% of the loan. Most mortgages don’t have these fees but they are out there, usually on adjustable rate loans.