Foreclosure is designed to allow for possession (or repossession) of property that was used to secure a debt that was subsequently unpaid. Most people simply think of foreclosure as "getting kicked out of your house," and in many situations that is an appropriate understanding. In reality foreclosure addresses ownership rights rather than possession, however. It involves the termination of at least one person's rights of ownership in favor of another person, and this can, but does not always, lead to eviction.
We don't think of it very often, but one of the great inventions of English law was the division of property into different property "interests" or rights that could co-exist in the same property. The state "owns" physical property in one way, the landowner in another, and the tenant also has certain ownership rights, for example. If the landowner is married, both spouses will have rights in the property, and it is possible to divide the rights up in many other ways, too. Another form of coexisting rights is the way the same property could be owned by you, but subject to a mortgage and also various sorts of liens.
It is with the mortgage and liens we are primarily interested here, because these can be "foreclosed." It is worth remembering that while most people (including the courts) only think of "purchase-money mortgages" (the mortgage you take out in order to buy your house) when they analyze foreclosure, there are other ways liens can be placed on your house (by the state for taxes or judgments, to name two), and all liens can be foreclosed. Mechanically what happens is that the foreclosing party causes the property interests to be divided and paid off - and the way that is accomplished is by selling the property and splitting the money up according to the priority of interests.
There is a definite hierarchy of interests, and the higher interests must be completely satisfied before the lower interests get anything. Eventually, if every interest is satisfied and money is left over, this would go to the property "owner." Or to put it another way, being the property owner means that you get whatever is left after all the other interests are paid off (you are entitled to the "equity"). But usually, if there is not enough to cover all the secured interests, you will owe the secured parties money personally.
Let's consider two examples. In the first, Owner A each own houses worth $100,000 on the open market. That's what it sells for.
Owner A has the following liens against the property: a purchase money mortgage of $35,000, a home equity loan of $10,000, and a mechanic's lien of $1,000. After subtracting all the debts, A is left with $54,000 - Equity
Owner B has the following liens against the property (in this order - the order of liens is beyond the scope of this article): a purchase-money mortgage of $110,000 (the house is "underwater" because the loan remaining is more than the house is worth); a home-equity loan of $10,000, and a mechanic's lien of $1,000. In B's situation, after subtracting all the debts, B is left with a negative number: -$21,000 equity.
If neither one can pay off the purchase money mortgage, go into default, and are foreclosed, here's what happens.
A loses possession of the house, and all security interests in the property are "extinguished." The money is enough for the mortgage, and that is subtracted and given to the bank. Because the home equity loan and mechanic's liens was "secured" by the house, the foreclosure breaches the contract with the lender. It intervenes (legally) in the foreclosure and demands its money and gets paid before anything goes to A. Because the lien was "subject" to the other agreements, it gets paid afterward, again before A gets anything.
In B's situation, the bank gets all the money, and the lenders are left with claims against B. Their security interests in the property are extinguished, and chances are good they'll lose everything they had lent.
What if, instead of not paying the bank, A and B had failed to pay the home equity loan? In that situation, the Home Equity lender could foreclose on the loan. Lower level security interests can foreclose on the loan. It would be conceivable that any other person with an interest in the property, including the mechanic, might take some action to intervene in order to protect its interests, although in B's case, especially, this is unlikely. The bank will get all the money, and the home equity lender will get nothing even though it is the one that foreclosed.
This explains why debt collectors rarely foreclose on a house. It will cost them money but get them nothing. But that isn't to say they couldn't or that it would never make sense for them to do or threaten to do.