Yes, you can contribute to your HSA as long as you are an eligible individual and have not enrolled in Medicare Part A, B, or D. Once you enroll in Medicare you may no longer contribute to your HSA.
For example, if you enroll in Medicare on July 21, you are no longer eligible to contribute to an HSA as of July 1. Your maximum contribution for that year would be for 6 months of that year (you were eligible the first six months of the year.) Remember to also include ½ of the catch-up amount for that year.
If you turn age 65 and are still working and are not enrolled in Medicare, you are still eligible to contribute to your HSA.