Opinions vary on this, but most recommendations I've read, and what I follow, is the following investment order:
- Establish an emergency fund to cover 6 months of expenses.
- If employer matches 401(k) contributions, max out their matching.
- Max HSA.
- Max Traditional/Roth/Backdoor Roth IRA depending on income level.
- Max 401k.
- Taxable investment account.
If you have debts, they often get prioritized after taking advantage of employer 401k match if they have high interest rates (guaranteed return, in essence), and many people don't worry about paying off longer term low-interest debts early, but some place them ahead of opening a taxable investment account.
The HSA piece is only relevant if you are on a high deductible insurance plan, the HSA popularity is due to the fact that contributions and distributions go un-taxed so long as the money is spent on qualified health expenses, and there is currently no time limit to claim expenses. This means you can pay expenses out of pocket and get distributions years later, thus benefiting from years of compounding tax-free growth. Some HSA's have poor investment options, so might have to go outside of employers, and the rules about reimbursement periods may change.