Yes, you can buy a house with existing tenants, but you need to understand your status. The landlord may sell his house even where there are tenants on it who have already paid rent to him. It is the responsibility of the purchaser (the new landlord to make such enquiries, as to the existence of tenants on the house).
Once you have purchased, you (as the new landlord) accept the existing tenants as your own tenants. If you want the tenants out of the house after you have bought, then you must give appropriate Notice(s). The tenants are therefore very well protected in respect of the rents already paid to the earlier owner/landlord.