The Protecting Tenants at Foreclosure Act of 2009 is a federal statute that allows most tenants in a foreclosed residential property to stay at least 90 days and in some circumstances, until the end of their lease term. According to this Act, tenants must receive at least 90-day written notice to vacate from the new owner. If the property has been sold to a person who intends to personally occupy it as his residence, then the new owner may terminate the lease after the 90 day notice. If, however, the property has been bought by a bank, investor etc. the tenants may remain in the property until the end of their lease term. Please note, that your lease must be a bona fide lease, meaning it cannot be between close relatives or at an amount below fair market rental value. If eviction proceedings have been started against you, I would strongly advise contacting a licensed attorney in your state.
This answer does not constitute legal advice and does not and is not intended to create an attorney-client relationship. The law may vary depending on the state in which you reside. It is intended only to give some direction in which to seek assistance.
The advice previously given is excellent advice. I would only add that in order for you to remain in the house after the Trustee Sale, for whichever period of time applies (whether the purchaser intends to personally occupy it as opposed to an investor or the lender by credit bid), you need to continue to pay the reasonable rent called for to the new owner, not to the foreclosed landlord.
Certainly, particularly if the lender took back the property, you should be positioned well to negotiate an accommodating rental rate after the lease term. The lender should prefer to be getting some money to help with HOA fees, taxes, insurance, etc. while deciding what to do with the property. The dilemma you have is that you may not be able to secure a stable time frame for living there. However, if you default in paying the rent called for in the lease to the new owner, you may well be evicted sooner.