Bankruptcy can stop foreclosure at least temporarily and, perhaps permanently, if you can successfully complete a Chapter 13 repayment plan while making regular mortgage payments.
The Chapter 13 sections of the Bankruptcy Code say that you can try to save your home by filing a Chapter 13 case BEFORE the sale of your home. Here is what the law says about curing a “default” (missed payments) in your mortgage:
a default with respect to, or that gave rise to, a lien on the debtor's principal residence may be cured under paragraph (3) or (5) of subsection (b) until such residence is sold at a foreclosure sale that is conducted in accordance with applicable non-bankruptcy law;
If you have received a foreclosure notice and have not already taken action to hire an experienced bankruptcy or foreclosure lawyer, you should do so well before the foreclosure sale date.