A few things I've learned over the years
- Many levels or only a few levels, for some reason the last level before a final boss is always one-too-many. I don't know why that is, but it has always worked for me that way.
- The more levels you put in the more variety is going to play a role. And I do not only mean purely in a game technical way, but also in a way a level looks
- Important is also how far are players sent back if they die.... This can also play a role when deciding a number
- When the game is strongly story based, it can make the number of levels significantly higher as a result, but that doesn't matter as long as the levels in combination with the story make sense. A few extra levels that are not really part of the story won't matter then as long as you don't overdo it, but what overdo means is rather a matter of feeling than cold hard numbers.
- Of course, if you have special plans but you don't dare to put them in as you think the number of levels is too high, why not make a few levels optional and making the player seek them out? Players who don't care can then skip the optional levels and the enthusiast can try to find them and as a result play them. Of course a few extra rewards for playing these extra levels is in order. This makes both groups happy (there are always whiners, but you gotta ignore them. You can't satisfy them anyway, no matter what you try).
Of course, it goes without saying these days, but I will still say it as it is very important. Make sure the player can save their progress. Especially when you have a crapload of levels. This is what I hate about the early Mario/Sonic games.... but back then it wasn't so common... Today it is, and I deem it very important too.