When you file a bankruptcy, the "automatic stay" goes into effect which prevents creditors from taking action to try to collect from you. The automatic stay will stop the foreclosure case. However, if you file a chapter 7 and there has already been a foreclosure filed, the mortgage lender will be able to get permission from the bankruptcy court (called "relief from the automatic stay") to proceed with the foreclosure case. So a chapter 7 bankruptcy will not save a house, but merely delay a foreclosure.
With a chapter 13 case, you propose a repayment plan to get caught up on the mortgage payments. The repayment plan will last 3-5 years. To save the house, you must be able to repay the arrears over the 3-5 years while also making the ongoing mortgage payments. The advantage to using a chapter 13 bankruptcy to stop a foreclosure over a mortgage modification is that the mortgage lender cannot say "no" to the repayment plan. As long as you can make the payments to get caught up, the mortgage lender doesn't have a say in the matter - unlike with a modification where they get to make the decision to offer a modification or not.