If you file for bankruptcy under Chapter 7, you give up property that is considered "non-exempt" in order to satisfy all or part of your debts. Exempt property is defined as property necessary for a debtor's support and the support of his dependents, if any. If you don't have any non-exempt assets to satisfy your debts, those debts will be discharged under Chapter 7 and you will be free of any legal obligation to pay them. So if you have credit card debt that is erased by the bankruptcy, you'll have extra funds that can then be used to pay or catch up on your mortgage payments.
Chapter 7 allows you to suspend your obligation to make mortgage payments, at least temporarily. When you file under Chapter 7, an automatic stay goes into effect, preventing a bank or mortgage company from foreclosing on your property. The automatic stay buys you time to stay current or get current on your mortgage obligations. However, mortgage companies often file a motion with the bankruptcy court for relief from the automatic stay, so it might not be useful for very long.
If you keep up or catch up on your mortgage payments by the time your bankruptcy case is closed, and continue to make mortgage, property tax and homeowners insurance payments, you can escape foreclosure as long as you continue to live up to your obligations under the mortgage. Sometimes your mortgage holder will ask you to sign a reaffirmation agreement wherein you agree to pay some or all of the debt on your house in return for the mortgage holder's agreement not to foreclose.
If you are "underwater" on your mortgage, i.e. you owe more than the equity in your house, signing a reaffirmation agreement is often a bad idea. Since your goal is to honor the mortgage agreement, and you can do so with or without a reaffirmation agreement by continuing to make the required payments, there is no upside to entering into one. If you do and later can't keep up with the mortgage payments, eventually losing the house, you'll usually owe the entire balance of the mortgage, even if the value of the house has significantly declined. However, it is unlikely a mortgage company will foreclose on a home after a Chapter 7 bankruptcy, whether or not a reaffirmation agreement has been signed, so long as the homeowner keeps up with his mortgage payments, property taxes and homeowner's insurance.