Before May 20, 2009, most renters lost their leases upon foreclosure. The rule in most states was that if the mortgage was recorded before the lease was signed, the lease would be wiped out when a foreclosure occurred. Tenants would then become month-to-month renters whose tenancy could be terminated with proper notice (usually 30 days, but 60 days in California).
On May 20, 2009, President Obama signed the Protecting Tenants at Foreclosure Act of 2009. Under this new federal law, leases aren't automatically terminated by a foreclosure. Instead, tenants have the right to stay until the end of their lease term, and month-to-month tenants must receive a 90-day notice before having to move out. One exception: if the new owner intends to occupy the foreclosed home (in other words, the new owner isn't the lender or an investor), the lease may be terminated with 90 days' notice.
The Protecting Tenants at Foreclosure Act of 2009 is currently set to expire on December 31, 2014. For more information on tenants' rights in foreclosure, see Nolo's article Renters in Foreclosure: What Are Their Rights?