How can I fix multiple layers of paint on window trim?
Removal is your best bet. Whether you use a heat gun, which much care will be needed since burning what will be bare wood eventually can easily occur. Also you will need to wear a respirator to get past any harmful fumes from the off gassing of the heated paint. Various shapes of scrapers will still be needed to clear out the crevices to freshen up the look of the existing trim. You have some nice trim there it would be great to reveal it from under all the layers.

There is another way top go, with paint remover. There is a brand that uses a cheesecloth that is imbedded into the remover and it allows many layers to lift off at one time. Something to consider and research.

The heat gun treatment will only need a fine sanding and dusting to get a paintable surface.
Some paint removers will need a neutralizer in some cases, rinsing in others. All depends on the products recommendations. Fine sanding and dusting is recommended here too. I give you a chance to check the quality of the removal before priming.