For example, a company owns a tower in NYC. In this tower there are several apartments that are sold to the public. If people end up buying these apartments, does the company now have less ownership of the building? What happens when the building is sold to some other company?
The short answer is the owner owns what it owns.
There are a variety of ways such a deal could be structured.
Condominium: Condominiums are made up of deeded titles to individual units. Each individual unit has partial ownership (and full rights to use/enjoyment) of common areas, and partial responsibility to pay for the maintenance of common areas. A board, representing unit owners, makes decisions about the common areas. A well-written condominium agreement will address exit cases such as eminent domain or if there is a fire and the cost to repair the building exceeds insurance settlements and other funds. If the agreement is poorly written, making decisions about the entirety of the building would be a legal disaster.
Condominiums can emerge as new construction or as a conversion of an existing building. There are examples of conversions of subset floors of whole buildings. Upon construction or conversion, the developer owns 100% of let's say 100,000 square feet of units, and 1000 square feet of common areas. After they sell a 10,000 square foot unit, they own 100% of 90,000 square feet, and 90% of the common areas. As other units are sold, they continue to own 100% of the remaining units, and a proportional ratio of common areas.
Cooperative: These are common in NYC and rare elsewhere. In a cooperative, the association owns the building outright, and residents own shares in the association, which confer the right to lease one unit. The cooperative association always owns 100% of the entirety of the building.
Open ended NYC is dense with value, so there are many non-uniform deals worth the legal labor to draft for a single case. A representative example is Penn Station/Madison Square Garden (MSG), where MSG was built over Penn Station, and the properties are 100% owned by separate entities (Amtrak and the MSG Company).
In a condominium situation, the individual units are owned separately. Some owners may rent their unit out, so not everyone in the building is necessarily an owner. The common elements of the building are owned by a developer or a community association, which assess a "condo fee" on the owners to pay for its expenses. There's also a condo agreement, which everyone who buys a unit must sign, that provides for the specific rules of the building - like defining which elements are "common" and which belong to the units, whether units are allowed to be rented, and how the condo fee will be determined and assessed.
Condo does not necessarily mean apartments in a single building, by the way. You can have communities that are a bit more spread out and still fit the condo ownership model.