Filing bankruptcy will automatically stay any foreclosure proceeding. So long as your bankruptcy case is filed prior to the actual foreclosure sale.
If your intention is to permanently save your home from foreclosure then you will need to catch-up your missed mortgage payments in chapter 11, 12, or 13.
In chapter 13 you will have to make a monthly payment for either 3 or 5 years. This payment must be enough to pay back the entire arrears on at least the first mortgage.
In some cases you are able to eliminate the second mortgage and keep your home.