There are many different ways to produce photons, but all of them use the same mechanism inside an atom to do it. This mechanism involves the energizing of electrons orbiting each atom's nucleus. How Nuclear Radiation Works describes protons, neutrons and electrons in some detail. For example, hydrogen atoms have one electron orbiting the nucleus. Helium atoms have two electrons orbiting the nucleus. Aluminum atoms have 13 electrons circling the nucleus. Each atom has a preferred number of electrons zipping around its nucleus.
Electrons circle the nucleus in fixed orbits -- a simplified way to think about it is to imagine how satellites orbit the Earth. There's a huge amount of theory around electron orbitals, but to understand light there is just one key fact to understand: An electron has a natural orbit that it occupies, but if you energize an atom, you can move its electrons to higher orbitals. A photon is produced whenever an electron in a higher-than-normal orbit falls back to its normal orbit. During the fall from high energy to normal energy, the electron emits a photon -- a packet of energy -- with very specific characteristics. The photon has a frequency, or color, that exactly matches the distance the electron falls.
You can see this phenomenon quite clearly in gas-discharge lamps. Fluorescent lamps, neon signs and sodium-vapor lamps are common examples of this kind of electric lighting, which passes an electric current through a gas to make the gas emit light. The colors of gas-discharge lamps vary widely depending on the identity of the gas and the construction of the lamp.
For example, along highways and in parking lots, you often see sodium vapor lights. You can tell a sodium vapor light because it's really yellow when you look at it. A sodium vapor light energizes sodium atoms to generate photons. A sodium atom has 11 electrons, and because of the way they're stacked in orbitals one of those electrons is most likely to accept and emit energy. The energy packets that this electron is most likely to emit fall right around a wavelength of 590 nanometers. This wavelength corresponds to yellow light. If you run sodium light through a prism, you don't see a rainbow -- you see a pair of yellow lines.