There are three basic transaction structures involved in virtually all buying/selling of companies. There are a few random ways, but this is a post, not a treatise.
Each of these transactions can have very different tax effects based on the entity and tax type of the constituent parties. Taxes, plus some standard business considerations, greatly affect choice of structure.
A merger – the buyer merges the seller company into another company (typically a new subsidiary of the buyer). All of the contracts of the seller move automatically to the successor entity along with all of its known and unknown assets and liabilities.
A sale of assets – the buyer acquires only specific assets and liabilities, typically including IP, tangible assets, certain employment agreements (non-competes), customer contracts, and material contracts with suppliers or vendors. Contracts have to be assigned to the buyer. Bank loans are typically terminated. Obligations to pay money or agreements with insiders are usually not assumed by buyers. Buyers typically do not want to assume any liabilities that are not needed for the benefit of the business going forward.
A sale of stock – this is like a merger except there is no new entity involved. It is used if a merger is not appropriate and an asset sale is not appropriate because there are contracts that cannot readily be assigned (such as a favorable contract with a supplier that provides great value to the company and that the supplier would renegotiate if it could). Also, there can be tax advantages to the seller to a stock sale over an asset sale (plus all liabilities stay with the company).