When a company goes into bankruptcy, they still have assets (inventory, a brand, real estate, customer contracts) -- those things just belong to their creditors now. The new owners need _someone_ to manage selling all those things to recoup some money.
More often, though, the business is still worth more than the sum of its parts. It just wasn't making enough to pay the loans it'd taken out. Since the loans get eliminated in bankruptcy, the new owners might decide to just keep running the company (and want a new CEO, who they think will do a better job.