Before I explain how you can be liable for taxes when your home is foreclosed, allow me to mention a certain Act that is relevant to this matter. The Mortgage Forgiveness Debt Relief Act states that any tax liability incurred from a foreclosure of any property will be waived if the debt was accrued from buying or improving the property. Under normal circumstances, that would include all property bought with a housing loan.
However, due to slick marketing on the part of the banks or personal reasons on the part of the borrower, the housing loan may not only comprise the value of the property. Some banks want to make more money by lending more to you so they are willing to absorb some of your other debts such as your credit card debts into your housing loan. Say, for example the value of your property is $200,000 and you pay a 10% down payment and take a loan for the balance of $180,000. The bank may offer you a loan of $200,000 instead by adding your credit card debt of $20,000 (i.e. the bank pays off your outstanding credit card balance and includes that debt into your mortgage). It sounds like a win-win situation where the bank lends you more money (and thereby earns more interest) and you receive more money from your housing loan.
But should you default on your mortgage, the Mortgage Forgiveness Debt Relief Act does not cover the additional loan of $20,000. That is how you might be liable for tax on the $20,000 when your home is foreclosed on by the bank. Losing your home due to the foreclosure is bad enough, but having to pay tax on a home you no longer own is rubbing salt to the wound.
The only way to overcome this is to file a bankruptcy petition. But the tax liability cannot be waived unless you are insolvent at the time your home is foreclosed or the foreclosure happens during or after you file for bankruptcy. So if you have assets like a retirement fund, you are not considered insolvent and you still have to pay the tax.