Why do X-rays and gamma rays pass through matter?

My understanding is that in general, the lower the frequency, the better the penetration of an EM wave. ULF waves can pass through oceans but higher frequencies are stopped by a tree or a building. So how do the very highest frequencies suddenly start gaining penetration power?

Higher frequency E/M waves have a lower probability of being absorbed by matter, and so they tend to just “pass through” it more, on average, than lower frequency waves like visible light.

No doubt a physicist or Health Physics person will be along shortly to explain it properly but the general answer is “they don’t”. You mention ULF radio waves passing through oceans but a few metres of water or concrete will make a pretty effective shield for gamma rays.

They are penetrating as compared to alpha particles and beta rays (stopped by paper or a thin sheet of metal respectively) but nothing like low frequency radio waves.

The best way to answer this question is in terms of the phenomenon of resonance. Basically, most objects have a certain set of frequencies that they “like” to oscillate at; these are called resonant frequencies for the object. If I apply a back-and-forth force to such an object, and the frequency at which I oscillate the force is near a resonant frequency, the object is going to oscillate quite a bit, and will gain a lot of energy from me. If, on the other hand, the frequency of my force doesn’t match up well with the object’s resonant frequencies, it might oscillate a little bit, but won’t gain very much energy from me.

So what does this have to do with EM waves? Well, EM waves (as you probably know) are composed of electric and magnetic fields. When these fields hit a molecule in some chunk of matter, the electric and magnetic fields in the wave will apply a back-and-forth force to the charges in the molecule. These molecules have resonant frequencies of their own, and if the EM wave happens to have a frequency near one of these resonant frequencies, the molecules will absorb a lot of the energy of the incoming wave, thereby not allowing it to penetrate very far into the material. If the EM wave’s frequency is far away from the resonant frequencies of the molecules, it won’t get absorbed as much, and will penetrate farther into the material.

Now, it turns out that most molecular resonances range are in the infrared to ultraviolet range. (Why this is so is really due to quantum mechanics, but it can basically just be considered a fact about how our Universe is put together.) This means that EM waves whose frequencies are not in this range—e.g., ULF, ELF, radio but also X-rays and gamma rays—will be able to penetrate a fair distance through matter, since the molecules don’t absorb much of the waves’ energy in their own vibrations. It’s only waves in the infrared to ultraviolet range that (for the most part) get absorbed by matter, because the molecules that comprise them “like” to oscillate at those frequencies.