You've surely heard you should stash money away in a rainy day fund for any unexpected expenses. Whether you lose your job, the refrigerator breaks, or your kid gets hurt at soccer practice, it's important to keep a few thousand dollars in the bank, just in case the unexpected happens.

While all those costs could dent your budget, there's one expense that nearly half of Americans are worried will bankrupt them: healthcare.

Around 45% of Americans said a major health-related expense could potentially lead to bankruptcy, according to a Gallup poll. Healthcare expenses can break the bank at any age, but they're especially detrimental to older Americans -- retirees in particular.

Not only are older adults more susceptible to health issues, but retirees usually live on a fixed income. And going bankrupt from healthcare costs is even more likely for the 42% of baby boomers who have nothing at all saved for retirement, according to an Insured Retirement Institute survey

IOU paper in wallet

Image source: Getty Images.

How Medicare factors into healthcare in retirement

Nearly three-quarters (72%) of Americans admit that they don't fully understand how Medicare works, in a survey by the Nationwide Retirement Institute. Even more worrisome is that more than half of survey participants wrongly believe all Medicare coverage is free.

If you're planning on relying on Medicare to take care of all your healthcare needs without paying a penny out of pocket, you may be in for a nasty surprise. While most Medicare beneficiaries won't pay anything for Part A coverage (assuming you've worked and paid taxes for at least 10 years), you will need to pay a premium for Part B coverage -- at least $135.50 per month, and possibly more based on your income. With both Part A and Part B coverage, you're also responsible for paying all deductibles and coinsurance for healthcare costs you incur.

In addition, original Medicare (Parts A and B) doesn't cover most routine care, including dental care and eye exams. For prescription drug coverage, you'll also need a separate Part D plan. Many people opt for a Medicare Advantage plan, which is similar to the type of insurance you likely have through your employer. Rates vary based on your location, age, and general health, but you'll typically pay higher premiums for more coverage.

Healthcare in retirement doesn't come free, and it may even be more expensive than what you're paying now. Long-term care isn't covered by Medicare and it can easily cost thousands of dollars per month if you or a loved one needs it. So if you're not preparing for those costs now, there's a good chance healthcare expenses could quickly drain your retirement fund.

Planning for healthcare expenses before you retire

The best time to start preparing for healthcare costs in retirement is long before you actually retire. If you wait until you leave your job to consider how you're going to pay your medical bills, it may be too late to put away a significant sum.

One way to start saving for future medical expenses is by opening a health savings account (HSA). An HSA is only available to people who have a high-deductible health plan (HDHP), which means your plan has a deductible of at least $1,350 for individuals or $2,700 for families. But if you qualify, you can contribute up to $3,500 per year (or $7,000 for families).

The beauty of an HSA is that your contributions are tax-deductible up front, and you also don't have to pay taxes when you withdraw the money as long as you're spending it on eligible medical expenses. Your HSA dollars can go toward premiums, deductibles, coinsurance, or any other related healthcare expenses. You can invest the sum you save and the money never expires, so you can save it for retirement if you don't need it before then.

While it may seem obvious, another way to prepare for healthcare expenses as you age is to take care of yourself. People in poorer health spend an average of $1,700 more per year out of pocket on healthcare costs than those in good health, according to a report from the Kaiser Family Foundation.

Those who are in poorer health may also be more likely to spend a significant period of time in a nursing home, which can potentially cost hundreds of thousands of dollars. (The average semiprivate room in a nursing home costs around $6,800 per month, according to the U.S. Department of Health and Human Services.) By taking care of your health now, you're making a long-term financial investment.

There are plenty of expenses to think about in retirement, but healthcare costs are one of the most important. Understand how much you may spend on healthcare in retirement and plan for these costs well in advance, and your wallet will thank you.