One issue; as you expressed it, there are several possible meanings for "Company A buys Company B":
Two different companies, A controls/owns B
Company A may buy all (of a portion big enough) of the stock of Company B to control it. Both Company A and B continue to operate as separate entities, from the POV of Company A, Company B (or its stocks) are an asset, from the POV.
Nothing changes, because Company B obligations must still honor Company B contracts.
Company A is merged with Company B
In this case, it is not really relevant if Company A buys B, the other way around, or it is just a merger by common accord. The important part here is that a company its both its assets and its obligations, so you can not get the assets without getting the obligations, too.
If that were not the case, fraud would be extended. Have company A ask for payments in advance, or a loan, and then sell company A to company B, that keeps the money but does not have to provide the goods or return the loan. Certainly does not sound like a working solution, isn't it?
Of course, company B has some protections to avoid buying a company with hidden obligations. In the process of buying/merging with Company A, B will request A information about its business, and A must be honest about it.
For example with accounting, Company A books should show data like:
how many assets it has.
how many obligations it has (debts, money paid in advance by customers).
its previsions of how that would change (if there is a customer that owns money but it is expected to become bankrupt, that debt should not be counted as an asset).
If Company A lies to Company B about its true situation, there have been situations in which the judges have reversed the operation or demanded compensation to the previous owners of Company A.
Company B becomes bankrupt and Company A buys its assets
Note that a very different situation is that of bankruptcy and liquidation of Company A. In this case, Company A is not bought (as it ceases to exist) and its assets are auctioned to pay creditors. In this case, the assets are bought separately, and the only obligations that the buyers accepts are those linked to the asset.
For example, Company A declares bankruptcy. They own just their trademark and a building, and own $1.000.000 to several debtors and the building has a $500.000 mortgage. The assets of A are put on sale, for whoever offers the most.
If Company B buys the building, it will accept the $500.000 mortage attached to it. But, even if it buys all the assets for $200.000, Company B will not have any obligation towards the $1.000.000 outstanding debts; those debtors will only be entitled to the $200.000 that company B paid for the assets1
TL/DR One person's obligations are someone else's rights. Those rights cannot simply dissapear unless the right holder agrees to or a legal process is followed. If someone gets transfered the rights/assets of one person (adquisition or merger for legal persons, inheritance for physical persons) he also gets the obligations.
1At least from Company B. They may try to investigate how Company A was managed and, in case there are proofs of fraud or negligency, sue the previous managers.