FSD agreed… if the landlord sells a home he does have the liability to stick to the lease contract that he had originated with the tenants. Sometimes landlords will buy out the lease agreement of tenants or the lease is transferred to the new owner and the deposit money is conveyed in escrow from current landlord to new landlord. Remember though, if you are a tenant and your lease contract has expired you are essentially on a month to month agreement.
In the case of a foreclosure I am pretty sure all bets are off.