I am on the "they sound different" side of things. I do not at all think headphones are or should be inherently better. However, it is possible that most decent headphones of a given price range will beat most earphones of the same price range due to a couple of reasons: 1) Headphones have been around for longer, 2) The vast majority of music is recorded and mixed for speakers.
In my opinion, sounstage and "realistic" presentation generally goes like this: Speakers>Headphones>IEMs, in a given price range.
Detail goes the opposite: IEMs>Headphones>Speakers.
However! As I mentioned, most music is mixed for Speakers. If we turn to HRTF binaural recordings, high quality ones ones which are few and rare, things shift substantially. Then, imo IEMs>=Headphones>Speakers, in terms of soundstage, and all about equal in presentation because speakers have the advantages of providing full body bass, and Headphones are a bit more palpable in bass than IEMs are due to their size. In good binaural recordings, positioning is much more accurate with headphones and IEMs than with speakers. If recorded with something like the Neumann KU 100 dummy head, then IEMs seem to have even better positioning and realistic soundstage than headphones, because the microphones are in the ears of the dummy head.
So what I'm saying is that I don't think any medium is inherently substantially better than the other, but the way most music is made favors reproduction through speakers, which headphones are closer to so they tend to reproduce better.