In visibly colored compounds the color results from the absorption of electromagnetic energy, in the form of photons, of particular energies. Blue compounds tend to absorb the energy in the red end of the spectrum, for example. The actual absorption is done by the electrons in the chromophore. After the energy is absorbed, the electrons are promoted into higher energy levels with the energy difference being equivalent to the energy of the absorbed photon. The electrons return to their ground state by some process, usually by thermal vibrations but potentially by emission as well. This emission (fluorescence or phosphorescence) can contribute to the color as well. Day-Glo dyes tend to absorb in the UV and emit in the visible, for example.
Why do colored compounds only absorb certain wavelengths? Think of the absorption process being coupled to the excitation process. Only certain energies are capable of promoting the electrons into certain excited states. This was explained to me as being similar to pushing someone on a swing. You have to time it just right... have just the right frequency. Spastic pushing doesn't get you anywhere.