A sale does not cancel the lease. The buyer becomes the new landlord, and the tenant stays under the existing lease.
The new owner is responsible for returning the security deposit starting on the day title transfers. The new owner must give each tenant a signed statement saying it bought the property and is responsible for the deposit, with the exact dollar amount.
You remain responsible for deposits you collected until the new owner receives them or takes over that responsibility, unless your written contract says otherwise. We discuss how the deposit is credited or transferred at closing.
For a month-to-month tenancy with monthly rent, either side may give notice. The tenancy ends on the later of the date in the notice or one month after the notice is given. A lease signed by both sides can set a different notice period.