Home mortgages get special treatment in bankruptcy. The mortgage company gets extra protection, but the debtor also has certain special rights. In a Chapter 13
you have the absolute right to keep the house if you can do these two things: (1) Keep making your regular payment on time every month; and (2) pay back the full arrearage (the amount that you are behind at the time of filing), plus interest, over the next 3 to 5 years. The regular monthly payment is just that; the payment that you would be making if nothing else had gone wrong. The arrearage includes all missed payments to date, plus any penalties, interest, late fees, attorney fees, foreclosure expenses, etc., that they have the right to charge up until that point. Your Chapter 13 Plan will treat the ongoing payments and the arrearage as two separate things.
In Chapter 7 it is safe to say that you can keep the house if: (A) you are current on the payments; and (B) the property is not worth a great deal more than the total amount of debt that is attached to the property (mortgages, property taxes, and other liens). There are two different concerns in Chapter 7: the mortgage company and the Chapter 7 Trustee.
You already know about the mortgage company, and the analysis there is more or less the same as it would be outside of bankruptcy. If you are current on the payments (including property taxes and homeowners insurance), then that's all they care about. It costs them a lot of money to process a foreclosure, and they are not going to foreclose just because your filed bankruptcy (especially in today's real estate market). But the farther behind you are on the payments, the less likely they are to agree to it. If you file a Chapter 7 hoping to work something out with the mortgage company, but they don’t agree, then you could lose the house.
If you want to surrender the house (or any other secured property), you have the absolute right to do that in both Chapter 7 and Chapter 13. In a Chapter 7, you would also walk away from any debt which you might still owe on the house. (See section on Repossessions and Foreclosures for more discussion about what happens to the property and to the debt.) In a Chapter 13, you might still end up owing some money to the mortgage company, but it would no longer be a secured debt; it would become a general unsecured debt. (See FAQ on the Difference between Secured and Unsecured Debt.)
Note: We are living in strange times, and mortgage companies are doing some strange things; some good and some bad. On the one hand, many mortgage companies have been caught padding the bill and taking unfair advantage of people in all sorts of ways. On the other hand, it used to be unheard of for a mortgage company to postpone a foreclosure or to go out of their way to work with customers, but those kinds of things have been happening with a certain amount of regularity lately. To be clear, I still wouldn't trust a mortgage company as far as I could throw them, because I spend roughly half my time fighting against the insane things that they have done to my clients. The point is that, for whatever the reason may be, there are times when they take much longer to foreclose than they once did, or when they will actually work things out. There have also been a few times when we caught them red-handed at something, and were able to sue them or otherwise use that as leverage to persuade them to make a deal. There's more than one way to skin a cat.