There is a landlord L, with a tenant T, and the landlord sells the house to a buyer B. The fact that T and B are the same person is irrelevant.
Usually people buy a house either to live there, or to make money by renting it out. The first type of buyer will want to buy a house without a tenant, the second type of buyer will quite happily take a house with a tenant. So that will be in the contract, wether you buy a house without or with tenant.
If the buying contract says "no tenant", the landlord L has to cancel the rent contract with T, and that will usually require paying back the security deposit to T, depending on the rental contract. If the buyig contract says "with tenant", then the landlord L sells the house together with the rental contract. T is not involved in this. The contract will usually say when T will pay starting rent to B instead of L, B or L will pay part of that rent to the other party, and L will give the security deposit to B.
On the other hand, there is the fact that L and B have been negotiating about the sale price. They can put anything they like into the sales contract, and the sale price will be different depending on the terms in the contract. It may be unwise to negotiate too hard about a few hundred dollars for the security deposit instead of haggling about the sales price and possibly saving several thousand dollars there.