If you buy a home anywhere in CA with a tenant on a lease, you are obligated to honor the terms of that lease, including letting the tenant stay until the lease expires. In San Francisco, there are additional tenant protection laws that are quite complex. They cover how you can regain "possession" (getting the tenant to move), whether you can increase the rent, etc. These rules vary based on the year the property was built, whether it is a single-family, condo, or multi-unit. This kind of complexity is one of the reasons that working with an experience buyers' agent who knows the local market is so important.
I'm happy to give you more details, or discuss a particular property or situation with you if you'd like. Just let me know.