If judgment of foreclosure has been granted and the sheriff's sale is immenent, you can stop it by filing bankruptcy. However, if your goal is to try to save your house, it is crucial that you d this correctly or it will not work. The bankruptcy law is technical and complicated, and people who try to do this on their own often make serious errors. The only bankruptcy chapter that would be anything other than a temporary stopgap is Chapter 13. There is a pre-filing counselling course that is required also. Time is of the essence.
The National Association of Consumer Advocates (NACA) is a non-profit consumer advocacy organization. NACA maintains a web site at www.naca.net where it lists geographically consumer law attorneys all over the US. If you don't already have an attorney, please look there for someone in your area who specializes in consumer bankrutpcy and mortgage foreclosure defense to review the details with you and advise you.
Bankruptcy will pause a foreclosure, but unless something changes in your economic circumstances, foreclosure and eviction is inevitable. An often overlooked option is to seek a mortgage modification from the lender. There are many non-profit agencies and organizations that will do this for free. Many are successful at stopping sheriff's sales as well.
Additionally, if you attempt to seek a modification on your own, you should ask your lender to withdraw its sale until they have an opportunity to review your loan. In order to be considered for a modification you will be asked to submit a "hardship package", which will include bank statements, income information, and a detailed budget. Lenders are more readily entering into modification agreements because of the number of foreclosures. As much as it may seem like it, lenders do NOT want your home. They would rather have some income from a homeowner than to foreclose and have an empty home on their books for 1-2 years.