From a budgeting perspective, the emergency fund is a category in which you've budgeted funds for the unexpected.
These are things that weren't able to be predicted and budgeted for in advance, or things that exceeded the expected costs.
For example you might budget $150 per month for car maintenance, and typically spend some of it while the rest builds up over time for unexpected repairs, so you have a few hundred available for that.
But this month your transmission died and you have a $3,000 bill. You'll then fund most of this out of your emergency fund.
This doesn't cover where to store that money though, which leads me to my next point.
Emergencies are emergencies because they come without warning, without you having a chance to plan.
Thefore the primary things you want in an emergency fund account are stability and quick access.
You can structure investments to be whatever you think of as safe or stable but you don't want to be thinking about whether it's a good time to sell when you need the money right now.
But the bigger problem is access. When you need the funds on a weekend, holiday, anytime outside of market hours, you're not going to be able to just sell some stocks and go to an ATM.
This is the reason why it's recommended to have these funds in a checking or savings account usually.
The reason I mentioned the budgeting side first is because I wanted to point out that if you're budgeting well, most of the unexpected expenses you have should have been expected in a sense; you can still plan for something without knowing when or if it will happen.
So in the example of a car repair, ideally you're already budgeting for possible repairs, if you own a home you're budgeting for things that would go wrong, budgeting for speeding tickets, for surprise out of pocket medical costs, etc.
These then become part of your normal budget: they aren't part of the emergency fund anymore.
The bright side about budgeting for something unexpected is that you know what that money is for, and do you likely also know how quickly you'll need it. For example you know if you have unexpected medical costs that happen very quickly, you're not likely you need a bag of cash on a moment's notice.
So those last two points lead to the fact that your actual emergency fund, the dollars that are for things you simply could not foresee, will be relatively small. A few thousand dollars or so in most cases.
If you've got things structured like this, you'll be happy to have a few grand available at a moment's notice.
The bulk of the money you would use for other surprise expenses (or things like 6 months of living expenses) is represented in other specific categories and you already know the timeframe in which you need it (probably enough time that it could be invested, risk to taste).
In short: by expecting the unexpected, you can sidestep this issue and not worry so much about missed returns on the emergency fund.