Yes you can sell the property while it is being rented.
The new owner has to honor the current lease agreements. Expect to provide copies of the leases to the potential buyer so they can review the contracts before committing to the purchase.
The current owner isn't obligated to inform the renters that the property is for sale, though being able to show rented units would require notification in advance. The rules for notification may be discussed in the lease documents, or local law.
It is not unheard of for the new owners to offer incentives to the current renters to either leave early, or to encourage them to not renew. This is so they can either renovate a building or convert the building to another use.
Renters in a month-to-month mode have to understand that they could receive their notice to not extend at any time, with the notice following the rental contract provisions.