Whether you contribute to a 401(k) that's run by your employer or are currently retired and withdrawing money from a 401(k), it's important to understand the tax structure of these accounts. Here's an overview of how the tax rules of 401(k)s work, and how to maximize their tax benefits for you.

Contributions to a 401(k)

In most cases, 401(k) contributions are made on a pre-tax basis. In other words, the money you contribute to your 401(k), up to the annual limit, is not subject to tax. For example, if you are paid a salary of $50,000 and contribute $5,000 to a 401(k) as a pre-tax, or traditional, contribution, your income from your job will be $45,000 in the eyes of the IRS.

Piggybank in front of chalkboard with 401(k) written on it.

Image source: Getty Images.

On the other hand, contributions to a Roth 401(k) are taxable. The money you contribute will still be included in your income when you receive your W-2, and therefore will be included in the determination of your taxable income. Unlike a Roth IRA, there is no income restriction for contributing to a Roth 401(k).

For the 2017 tax year, you can choose to defer $18,000 from your salary into your 401(k) plan, with an additional $6,000 allowed if you're over 50.

If your employer has a matching contribution program, it doesn't matter which type of contribution you make, traditional or Roth. All employer contributions will always be made to the traditional (pre-tax) side of your 401(k).

The Saver's Credit

Regardless of whether your 401(k) contributions are of the pre-tax or Roth variety, if your income is below $31,000 in 2017 ($62,000 for a married couple filing jointly), there's an additional tax break that you should be aware of.

The Retirement Savings Contributions Credit, which is also known as the Saver's Credit, is designed to incentivize retirement savings for low- to middle-income taxpayers. Depending on your income level, you could qualify for a credit worth 50%, 20%, or 10% of your qualified retirement contributions up to $2,000 -- and 401(k) contributions qualify. For married couples who qualify, each spouse can take the credit.

Taxes on 401(k) withdrawals

If you withdraw money from a 401(k), the tax implications depend on whether the contributions were made on a pre-tax or after-tax (Roth) basis.

Pre-tax contributions are far more common, especially among older workers' and retirees' 401(k) accounts. Withdrawals from pre-tax 401(k) accounts are considered to be taxable income, since tax was never paid on the money when you first earned it. These withdrawals will be added to any other income you've earned for the year, and after your deductions, exemptions, and credits are accounted for, will be applied to the 2017 tax brackets, just as if you had earned the income from a job this year.

On the other hand, any money in your 401(k) that resulted from Roth contributions can be withdrawn tax-free, provided that the withdrawal is qualified (you're 59 1/2 or older, and the account has been open at least five years).

When you take a distribution from a 401(k), your employer is required to complete and file a Form 1099-R with the IRS, which shows the IRS how much was distributed, how much tax (if any) was withheld from the distribution, and information about whether you're older than 59 1/2 years old or not.

What if you withdraw money from your 401(k) early?

In general, using 401(k) savings before you reach the age of 59 1/2 will trigger a 10% penalty from the IRS. There are a few exceptions to the penalty, such as the exclusion for Roth contributions, or any withdrawals after age 55 if you're no longer working for the employer.

In addition to a possible penalty, all pre-tax (non-Roth) contributions and any investment profits you withdraw from your 401(k) early are counted as taxable income.

This can have major tax implications, especially if you're considering cashing out your 401(k) after leaving a job. Specifically, doing so can catapult you into a higher tax bracket, which you'll have to pay in addition to the 10% penalty, if it applies. Depending on how much you have in your account, cashing out a big 401(k) could potentially result in a marginal tax rate of nearly 50%, and that's not including any state income taxes you'll have to pay. For this reason, cashing out a 401(k) is rarely, if ever, a smart financial move.