If you are facing foreclosure, filing for bankruptcy can give you at least a temporary break. As soon as you file for bankruptcy, whether under Chapter 7 or Chapter 13, a court order called the automatic stay goes into effect, which prohibits creditors from taking any actions against you to collect most debts. So, foreclosure proceedings will come to a halt, thanks to the stay. However, a creditor can ask the judge to "lift" the stay and allow it to proceed with a foreclosure -- and the judge is likely to grant the request if it looks like you won't ultimately be able to keep your home.
In Chapter 13 bankruptcy, you must agree to a repayment plan to pay off some or all of your debt over three to five years. If you have missed some payments on your home, you can include the arrearage in your plan and pay it off over time. As long as you complete your plan and stay current on your monthly payments, you will be able to keep your home. The automatic stay will prevent foreclosure proceedings while your case is active; once it's over, you will be back on track and the lender will have no reason to foreclose.
Unlike Chapter 13, Chapter 7 bankruptcy doesn't require a repayment plan. Although this makes it the more popular option for many bankruptcy filers, it doesn't give homeowners a way to get back on track with their mortgages. If you want to keep your home, you will have to come up with what you owe or negotiate different arrangements with the lender.
Chapter 7 puts your home at risk in another way: Even if you aren't behind on your payments, you could lose your home if you have substantial equity that you can't protect with an exemption. Most states provide a homestead exemption, which allows you to keep some or all of the equity in your home. However, if the exemption available to you doesn't cover all of your equity, the trustee can take your home, sell it, pay off the mortgage, pay you your exemption amount, and distribute the rest of the proceeds to your unsecured creditors. (For more on exemptions, see Bankruptcy Exemptions - What Do I Keep When I File for Bankruptcy?)