Back to the OP: There are two kinds of residential mortgages in the US: recourse and non-recourse. Some states only permit recourse loans, whereas other states have both kinds.
In a recourse loan, the house is collateral for the loan. If the buyer can’t / won’t pay, the lender can (not must) seize the house (foreclose). The lender then sells the house for whatever they can get for it. If it sells for more than the loan amount, the lender gives the excess back to the borrower (net of costs). If it sells for less than the loan balance (good bet in most markets today) the lender can (not must) sue the borrower for the shortfall.
A non-recourse loan is exactly the same up until the moment the house sells for less than the loan balance. In a non-recourse loan, the lender has no ability to sue the borrower. They have to eat the loss, pure and simple. And the borrower has zero legal obligation to make up the bank’s shortfall. Walking away is 100% legal. Why is it legal? Because the contract they both signed when the loan was originated said they agreed to do it that way. The lender agreed to take 100% of the risk of coming up short.
In either case the borrower’s credit rating will take a hit.
But you can certainly see that if you had a bunch of other assets (e.g. stocks, bonds, etc), you’d be more inclined to stick with your loan if it was a recourse loan and less inclined if it was a non-recourse loan.
By some strange coincidence (not), non-recourse loans were legal is states which had the biggest booms and are now having the biggest busts. They’re illegal in most states for residential mortgages for the obvious reason.
If you have A) assets, B) a house underwater, and C) a non-recourse loan, then the only sensible, profitable, economically rational thing to do is walk away.
As **Duckster **points out, the problem comes in when somebody else, or a lot of somebody elses do it. As with so much in economics, if everybody acts in their own pure self-interest, the outcome is not some prosperous Adam Smith libertarian paradise, but rather a self-reinforcing race to a collectively disastrous bottom.