Electrons orbit the nuclei of atoms at specific "orbitals" which each correspond to an "energy level" meaning that for any electron to gain energy, it must move to a higher energy level and to lose energy, it must move to a lower energy level. Each element and each molecule has only certain energy levels available, and so an electron orbiting any nucleus can only go from its current level to one of the other available levels. The differences between different pairs of energy levels restrict atoms and molecules to gaining and losing energy only in particular quantities, a fact known to be so fundamental to the structure of the universe that a major branch of physics, quantum mechanics, takes its name from these distinct quantities of energy gain and energy loss.Now, if you think that the Law of Conservation of Energy means that an electron cannot just gain energy or lose energy, then good! You're absolutely right. To go to a higher energy level the the electron has to get energy from its environment, and when an electron loses energy, that energy does not just cease to be, it has to go somewhere. We can't say where the energy will end up, but we do know exactly how it has to leave, and that is as a photon, aka, electromagnetic radiation. That's right, "aka" meaning "also known as" meaning that a photon is exactly the same thing as electromagnetic radiation. This is the infamous "wave/particle duality" of quantum mechanics, which some people find fascinating in its own right, but which is not central to The Greenhouse Theory. What is important to know about this for climate science is that changes in energy must occur in distinct quantities which are unique for each substance, and those energy level changes cause (or are caused by) emission (or absorption) of photons having the exact same amount of energy, and the quantity of energy per photon corresponds to a wavelength of electromagnetic radiation (some frequencies of which are visible, and called light).
This, finally, brings us to what makes some gases greenhouse gases. A range of per-photon energies just slightly less than that of visible red light, called the infrared range or infrared spectrum, also cause atoms and molecules to vibrate at frequencies corresponding to heat when infrared photons are absorbed. Likewise, warm gases that cannot emit infrared, stay warm. No molecules are unable to emit photons at any infrared wavelengths, but methane (CH₄), carbon dioxide (CO₂), water (H₂O) and all other greenhouse gases have "bands" within the infrared spectrum in which they do not emit electromagnetic radiation, making these molecules less able (not totally unable) to lose heat. That is what makes some gases "greenhouse gases."