You can sell while tenants remain, but a sale does not by itself end a Massachusetts residential tenancy at will or set a move-out date. The buyer becomes the landlord, and any written lease still matters. Ending a tenancy and getting possession are separate questions governed by applicable notice and legal steps.
At the sale, Massachusetts law requires you to transfer each security deposit and its accrued interest to the buyer, and credit the buyer for prepaid last month's rent and its accrued interest. Interest is 5% a year, or the lower amount the bank actually paid. Within 45 days after the transfer, the buyer must tell each tenant in writing that it now holds the money.
A proper transfer and written notice can release you from responsibility for the tenant's money; returning it to the tenant is another option. The buyer becomes liable to the tenant even if the money was not transferred. The closing handoff also includes the lease, deposit and prepaid-rent records, interest receipts, and tenant notices.
Read the state sources: G.L. c. 186, § 13 (sale and tenancy at will) · G.L. c. 186, § 15B (deposits, prepaid rent, interest, and transfer) · Massachusetts Attorney General’s Guide to Landlord and Tenant Rights.