Will filing for bankruptcy stop the lender from foreclosing on my home?

Filing a Chapter 7 case only temporarily sidetracks a lender's right to foreclose until it gets permission to go forward with the foreclosure proceedings by requesting and receiving "relief from the automatic stay" from the court. That relief is likely to be granted unless you can immediately bring your account up to date, demonstrate a likelihood and that you'll continue to make payments when due, and show that your equity in the home provides a sufficient "cushion" for the lender. In some bankruptcy districts, you must also negotiate a formal "reaffirmation agreement" with the lender.

A Chapter 7 never permanently stops a foreclosure, unless the creditor agrees and homestead (exemption) laws stop the trustee from selling the property.

Most people who file for bankruptcy have big arrearages on their mortgage that they can't pay off right away. The solution to that problem that allows them to keep their home is to file under Chapter 13 bankruptcy. The Chapter 13 plan provides for continuing monthly payments on the mortgage and paying off the arrearages over the life of the plan (three to five years). 

So, even though your mortgage does not change, your financial situation does. This means that you have more funds available so that you can afford to make the payments to keep your home.