As you might expect, I hear some version of this question frequently. My first answer is always "It depends". After that, I ask questions and discuss the possibilities. The questions and discussion below are a summary of those conversations.
The first question you should ask yourself is "Do I want to keep this house?" Maybe your answer is "Of course I do." But it shouldn't be "Of course." You should take a long, hard look at whether you can afford to keep that house, and even if you can afford the payments if it's a wise financial move. If you can't afford the payments, then even after a bankruptcy you'll still be in a bad financial situation each month and might lose the house anyway. If you can afford the payments, should you? Or would you be better off letting it go considering that it's worth less than you owe on it. Or, because of a very low interest rate on the mortgage loan, maybe your payments are less than rent for any place you would rent after you let the house go. These are all things you should think carefully about and discuss with a bankruptcy attorney as you consider whether bankruptcy would be right for you.
If you want to let the house go
A Chapter 7 bankruptcy might be valuable for you. It would get rid of your unsecured debts (like credit cards and other debts that are not secured by something like your house or car). It would not allow protect you from a foreclosure, but you've already decided to let the house go. It would protect you from any liability on unpaid loans after a foreclosure (a separate subject in itself).
If you want to keep the house
Consider a Chapter 13 bankruptcy in which you get to pay off the back mortgage payments monthly over three to five years by making payments to the bankruptcy trustee. You have to be able to show the court that you can afford to make those monthly payments. As long as you make those payments to the trustee and also your current mortgage payments, you can keep your home.
Obviously there's a lot more to this than the short discussion above. That's why you should discuss your situation with a qualified bankruptcy attorney in your area.