A Chapter 7
(liquidation of all assets) Bankruptcy will only slow down your foreclosure for about 60 days. A Chapter 13 (reorganization) Bankruptcy can sometimes stop your foreclosure for up to five years. Eventually, however, if you cannot catch up with your payments (or, in limited cases, if you have a 2nd mortgage, and your property is worth less than your 1st mortgage), then you will have to either sell the property, or suffer the foreclosure. The good thing about the Chapter 13 is that you can hope that your property value will increase during the duration of the 5 year plan, and if it does, then you may be able to sell the property to pay off the mortgage, so that you will never actually have been foreclosed.
Sometimes, however, it's better to just file Chapter 7, get rid of your debts, and move on with your life. Five years can be a long time, while you're living under the thumb of the bankruptcy trustee
Everyone's circumstance is different. But, most consumer bankruptcy attorneys provide free first consultations, so it's worth visiting with a local bankruptcy lawyer and discussing your full financial circumstances to see if you fit in one of the categories where bankruptcy could really make a positive difference.
For a competent bankruptcy lawyer referral, see this link.
Please let me know if you need further clarification or assistance.