In most cases, a foreclosure terminates the tenant’s leasehold interest and the bank can evict former owners and tenants through the courts, although tenants usually have more rights in this regard than former owners.
So, the bank should serve papers (e.g., eviction notices, summons, etc.) on the occupants, and if the occupants are tenants, then the tenants should appear in court and assert their rights as tenants.
Banks or new owners via foreclosure should follow the tenant rules for all post-foreclosure evictions to ensure they can evict anyone in possession, whether tenant or former owner. If the bank followed the proper procedures, then it gets a judgment for possession and the court issues a writ of possession to the sheriff.
I guess my point is that by the time it gets to the sheriff, all the rights of the parties should have been adjudicated (in an ideal world).