How do headphones and earphones produce good bass if tiny speakers can't produce low frequency sounds very well? It's a well known fact that small/tiny speakers cannot produce low frequency sounds very well. Conversely, large speakers cannot produce high frequency sounds very well. Hence the need for tweeters and woofers in your speaker systems.
But, how do the tiny speakers inside headphones and earphones produce good bass when placed over/in the ears? (I must add that not all of them do)
 A: Sound waves are pressure waves. In open air, the volume of air that you must move to produce a given compression is proportional to wavelength. But in a confined space much less than a wavelength in extent, only the air in that confined space needs to be moved, so you get the same compression for the same air motion, independent of wavelength.
A: There are a few reasons why small speakers have trouble creating bass.

*

*Bass is directly proportional to the amount of air the speaker can
move. So you want a large cone that can move a large distance. That's
hard to package in small box.

*A conventional cone loudspeaker has actually two sound sources: the front of the cone creates positive pressure while at the same time the rear of the cone creates negative pressure. These two pressures need to be separated otherwise they cancel each other. That's why you put a bass speaker in a box. The box contains the pressure from the back of the cone. That also means that the speaker needs to compress the air in the box and the air will push back. The smaller the air volume in the box, the harder it will push back and the more force is required to compress it. Interestingly enough, a small speaker spends a large part of it's energy to compress air in the box, not radiating sound to the outside world (which takes a lot less energy).

*Loudspeakers create a spherical wave where the sound pressure falls proportional to the distance. They are very loud when you are very close, but the sound pressure drops VERY quickly as you move away.

Headphones and ear phones have the advantage that they are very close to the ear, so you are operating still in the "VERY loud" part of the distance dependency.
Sitting on the head reverses the "box" problem. You only need to pressurize a very small volume while you can vent the back pressure to the outside world which is huger by comparison. Since the volume is so small, there is only very little movement required. One way to think about this: room speaker needs to pressurize the entire room, an ear phone only needs to pressurize the tiny little volume inside the air canal.
The bass response for both headphones and ear phones is very sensitive to the air seal between the ear cup or ear bud to the body. Any leakage will result in some cancellation of the front and back pressure. There are ways around this with so-called "open headphone", but that's probably to deep for this answer.
A: So, I'm no expert but I read about this some years back in a fascinating book called How Music Works (not David Byrne's), and it explained how sounds of a given fundamental frequency (its first harmonic, the frequency we would use to identify a particular note), will be recognized by our brain as the fundamental frequency, even if only the other harmonics are present, while the first is missing, which can allow small speakers with poor ability to reproduce low frequency sounds, to reproduce a low note without actually needing to reproduce the fundamental frequency.
The book uses A2 as an example because its fundamental frequency is 110Hz and it's a nice easy number. That makes its second harmonic 220Hz, and its third 330Hz, etc. And in a normal note, we hear all of these harmonics at once but in a way that repeats at 110Hz, so we can always identify it as that fundamental frequency.
Let me quote some of this before I completely mangle the explanation:

All these vibrations (with lots of others) happen at the same time, as a complex dance which repeats a whole cycle at the lowest frequency involved – 110Hz.

Some of this is also explained on wikipedia

Pitched musical instruments are often based on an acoustic resonator such as a string or a column of air, which oscillates at numerous modes simultaneously. At the frequencies of each vibrating mode, waves travel in both directions along the string or air column, reinforcing and canceling each other to form standing waves. Interaction with the surrounding air causes audible sound waves, which travel away from the instrument. Because of the typical spacing of the resonances, these frequencies are mostly limited to integer multiples, or harmonics, of the lowest frequency, and such multiples form the harmonic series.
The musical pitch of a note is usually perceived as the lowest partial present (the fundamental frequency), which may be the one created by vibration over the full length of the string or air column, or a higher harmonic chosen by the player.

So more from the book as to how this relates to the question at hand:

Look at this collection of frequencies. Together they make up our old
friend the note A2, which has a fundamental frequency of 110Hz:
110Hz, 220Hz, 330Hz, 440Hz, 550Hz, 660Hz, 770Hz etc.
As you know, the timbre of an instrument is made up of the various loudnesses of these ingredients within the ripple shape. Whatever the mixture of ingredients, our brain recognizes this as a note with an overall frequency of 110Hz. Even if the loudest, strongest component was 330Hz, the overall pattern would only be completing its dance 110 times a second – so the fundamental frequency is 110Hz.

Further:

Rather than just being a minor contributor to the sound it is possible that one of the harmonics could be completely silent. If, for example, the 770Hz frequency was completely absent, we would still hear the remaining harmonics as part of a note which has a fundamental frequency of 110Hz. This is because only 110Hz can be the head of a family which includes 110Hz, 220Hz, 330Hz etc. We could have several of the harmonics silent – and still the fundamental frequency would be 110Hz.
Now the odd bit: we can even remove the first harmonic, the fundamental – 110Hz – and the fundamental pitch of the note we hear would still be 110Hz. This sounds a little insane but it’s perfectly true. If you hear the following collection of frequencies: 220Hz, 330Hz, 440Hz, 550Hz, 660Hz, 770Hz etc. you will hear it as a note with a fundamental frequency of 110Hz, even though the sound does not contain that frequency.

And bringing it together:

Nowadays it is possible to get ridiculously low frequencies out of small speakers by utilizing the ‘missing fundamental’ idea. Let’s say your speaker won’t do much at frequencies of less than 90Hz, but you want to hear the note A1 clearly – and it has a frequency of 55Hz. If you feed the harmonics of 55Hz to your speaker without the fundamental (i.e. 110Hz, 165Hz, 220Hz, 275Hz) you will hear 55Hz loud and clear even though the lowest frequency at which your speaker is moving is 110Hz.

A: My guess is that an important thing here are near-field effects.
Part of the problem for bass emission from small speakers is the effect of acoustic short circuiting. As the membrane moves, the air moves around the membrane, cancelling the emission in the far field.
(Bass reflex boxes are a way to mitigate this limitation).
Headphones mitigate such by having damping material in the way of the acoustic short circuit. (Like the ear in the case of in-ear speakers, or the muff for normal headphones.) Additionally the distance between the ear and the speaker is on the scale of the size of the membrane, and on those short distances the near-field components of the acoustic field (that fall off with higher powers than $1/r^2$). This is the particle movement that is causing the acoustical short circuit – it falls off fast with increasing distance, but it contributes to the pressure close to the membrane.
The length scale where the near-field effects are relevant is related to the wave length – so the length scales at which the near-field effects are relevant is different for the high frequency and the low frequency parts.
A: It's actually because headphones/earphones are placed in directly contact with your ear and so the sound waves do not need to travel far. The intensity of sound decreases as $(\frac 1r)^2$, and so decreases rather rapidly as move away from the source.
Now because headphones/earphones they do not operate in open air (or very little air), they do not need to generate as much energy to produce the sound you hear from speakers at a distance. Head phones are in close contact with your ear drum and with little air in between, so a large speaker cone vibrating to create high amplitude (low frequency) sound waves (which we hear as bass) is not needed to produce the same sound effects that you get in headphones/earphones.
A: I challenge your premise that small/tiny speakers cannot produce low frequency sounds very well.
They can do so perfectly fine, just not at the volumes you need in free standing speakers.
When worn over or in your ear you do not need a lot of volume and that is the simple reason headphones and earphones produce good bass.
A: To try to summarize what others have said:
High frequencies require a speaker to pressurize small amounts of air quickly.  Low frequencies require a speaker to pressurize lots of air more slowly.  A headphone speaker handles both jobs for the small volume of air in your ear canal.  However, in a large space, you have to move much more air, which means you need larger speakers for low tones.    But those speakers aren’t great at moving fast, so you also need small speakers for the high tones.
Fall-off over distance is not particularly relevant in either scenario.
A: Just imagine you are small person standing in front of the earpiece. It will produce bass sounds as well as high frequency sounds. If you go stand very near the piece, the bass sounds will be felt by your stomach like a large-sized speaker in the world of grown-ups does. But because of the relative large motions involved in bass sound, it will dissipate its energy fast in air. That's why you don't hear the bass if you hold the earpiece far from your ear. Just as the bass sounds are gone or faded down if you listen to normal sized music boxes from far away.
