Your HSA belongs to you, regardless of what happens with your health insurance. But if you switch to a non-HDHP (or if you gain coverage under a second health plan in addition to your HDHP), you have to stop making contributions to your HSA at that point. You can still withdraw tax-free funds from the HSA to pay medical expenses, including out-of-pocket costs under your new non-HDHP health plan.
You can no longer contribute to an HSA once you’re enrolled in Medicare — even if, for example, you continue to work and have HDHP coverage from an employer, in addition to Medicare. But you can continue to withdraw tax-free funds from your HSA after you’re enrolled in Medicare, as long as you use the money to cover out-of-pocket medical expenses, including Medicare premiums (this is discussed in more detail below).