Would the court be able to order the gym to transfer said membership
to a creditor? Or perhaps pay out a pro rate lump sum to the creditors
in exchange for canceling said membership?
Would it matter if the lifetime purchase is for something more
substantial than a gym membership? I.e. do the rules change if you've
contracted Hilton to provide you a free room in any hotel for the rest
of your life?
What happens varies a bit depending upon the kind of bankruptcy. In a Chapter 7, all property and contract rights of the bankrupt debtor vest in the bankruptcy trustee when the bankruptcy petition is filed, by operation of law, with certain exceptions. (Chapters 11, 12 and 13 which are reorganizations are conceptually more complicated and I won't address them at length. But, in a reorganization, the title to the debtor's property doesn't necessary vest in a distinct bankruptcy estate at a single moment in time and instead can continue to be owned by the debtor subject to various bankruptcy related obligations.)
Certainly, a right to use a hotel for life, which is very close but not identical to a legal life estate in a time share, would be a bankruptcy estate asset and would be vested in the bankruptcy trustee.
The bankruptcy court is fairly limited in how it can adjust the rights of third-parties who are not creditors of the bankrupt, although it is not entirely without any authority to do so. It can invalidate "ipso facto" clauses in contracts that are triggered only upon bankruptcy, it can unwind preferential payments and fraudulent transfers made prior to filing for bankruptcy, the trustee can invalidate contracts that a third-party lien creditor could invalidate, they can declare restraints on the transferability of contract and property rights that are invalid under state law invalid, and so on. But, generally speaking, the property rights of a party are what they are defined to be unless a specific exception applies.
In some cases, if an asset is not exempt from creditors claims, but is also not transferrable, the bankruptcy court could probably compel the bankrupt debtor to buy that asset back from the bankruptcy estate at fair market value, on some sort of financing terms that made it possible to do so, so that the creditors of the estate are not harmed by the lack of transferability.
A fact pattern involving a membership in a Surf Club worth several hundred thousand dollars is explored in 2014 ruling in Feaster v. Surf Club, an adversary proceeding in a debtor-member's Chapter 13 bankruptcy, although it doesn't address all of the issues in this question.