When electromagnetic radiation (photons) meet matter, it produces different effects on molecules, atoms or even nuclei. The higher the photon energy (frequency), the more volatile the effect.
In turn, most of these effects produce EM radiation when releasing that energy.
The possible interactions are
When matter contains heat, its molecules behave chaotically in many ways, moving relative to and bumping into each other, themselves thus rotating and vibrating.
Their own vibrations as well as the vibrations of their individual atoms relative to each other, have only a limited number of ways of doing that and only with discrete frequencies. Both depend on their configuration and bonds. In other words, depending on the chemical structure, the sorts of behaviors (vibrations) as well as the number of energy levels are determined. The more atoms a molecule has, the more simultaneouss vibrations as well as sorts of vibrations can be going on. But even a simple molecule can have a lot of different energy levels!
Some sorts of vibrations: stretching, bending, rotating, twisting, rocking, scissoring and wagging . And each has different subdivisions.
Now, we can imagine molecules by their orbitals. The vibration of these electron clouds from their normal state, creates moving uneven electric fields.
Each specific transition between two specific vibrations have very specific energy differences, which corresponds to discrete frequencies. When a molecule "cools down", it goes back to a lower vibration state and gives up that discrete energy in the form of EM radiation with the same frequencies (or heat).
And vice versa, any object can also absorb heat when the specific frequencies of the radiation fits with the possible vibrations within the object.
There is a useful theoretical body that can absorb all frequencies. Our sun behaves much like it. See "black body" for more.
P.S. Some texts make a clear distinction between vibrating and rotating. Here, we didn't and don't.