Filing for Chapter 13 bankruptcy can be a good way to save your home from foreclosure. Chapter 13 bankruptcy lets you pay off a mortgage "arrearage" (late, unpaid payments) over the length of a repayment plan that is approved by the court -- usually between three and five years. In order for this option to work, you'll need enough income to at least meet your current mortgage payment at the same time you're paying off the arrearage. (To learn more about saving your home through Chapter 13 bankruptcy, see Nolo's article Your Home in Chapter 13 Bankruptcy.)
In Chapter 7 bankruptcy, if you have sufficient equity in the home, the bankruptcy trustee may sell your home to repay unsecured creditors. Given the state of the real estate market, this rarely happens these days. But if you're behind on your mortgage payments, Chapter 7 doesn't provide a way for you to catch up, and the lender will likely get permission from the bankruptcy judge to go ahead with a foreclosure. (To learn what happens to your home in Chapter 7 bankruptcy -- including how to determine if you have non-exempt equity -- see Nolo's article Your Home in Chapter 7 Bankruptcy.)
If your only goal of bankruptcy is to save your home, be sure to consider other alternatives first. You may be able to negotiate with your lender to deal with arrearages or get government help to modify your loan terms. To learn more about ways to save your home, see Nolo's article How to Avoid Foreclosure.