Bankruptcy may or may not stop an eviction, it just depends on your circumstances. The automatic stay that bankruptcy provides will protect you in many circumstances, but there are exceptions. Under section 362(b)(22) of the Bankruptcy Code the eviction of a debtor from a residential property in which the debtor resides as a tenant under a lease or rental agreement is not stayed if the lessor has obtained a judgement for possession prior to the filing of the bankruptcy petition, unless certain conditions are met. The debtor needs to list the name and address of the lessor in the bankruptcy petition. Additionally the debtor needs to file a certificate with the petition stating the debtor has a right to cure the monetary default under applicable non bankruptcy law and the debtor needs to deposit all rent coming due after the filing of the bankruptcy petition. This will extent the stay for 30 days. From a practicable stand point these requirements will be too burdensome for most debtors and as such the bankruptcy will not stop the eviction. If you find yourself facing eviction as a renter it is important that you file bankruptcy before your landlord obtains a judgment for possession.
If your home has been sold in foreclosure the automatic stay still applies. This means that the new owner of your property would have to file a motion for relief from stay in order to move forward with an eviction. In most cases the new owner of your property will just wait until the completion of your bankruptcy before moving forward with an eviction.