First things first: No matter how much it may sound like rolling waves, you’re not hearing the ocean in a shell. You’re picking up ambient noise that’s being produced all around you, which you normally don’t really acknowledge or pay attention to.
To amplify this ambient noise so you can hear it clearly, you need a resonator. Want to make one on the cheap? Form an O shape with your mouth and flick your finger against your cheek. You should hear a note. Make a smaller or larger O, and you’ll get different notes. You’re letting your mouth fulfill its potential as a Helmholtz resonator, in which sound is produced by air vibrating in a cavity with one opening.
The seashell you’re listening to—the inside of which has many hard, curved surfaces great for reflecting sound—is also a resonator. Ambient noise is resonating inside the cavity of the shell, becoming amplified and clear enough to notice. Shells of differing sizes and shapes won’t sound the same, because varying resonant chambers will amplify different frequencies.
The fact that all shells sound just a little bit like the ocean is coincidental. Holding any sort of Helmholtz resonator to your ear will produce a similar effect. Put an empty glass over your ear or even cup your hand around it, and the sound you hear will be just about the same, though it won’t feel quite as special. These mesmerizing photos of jellyfish will ignite more awe about the ocean!