The easy but not rigorous answer is that there is a resonance condition for transition of electron from one quantized orbit to another in response to an incoming photon . If the photon does not possess the right energy, it goes on its merry way - it's not absorbed and passes through.
I reality, the resonance condition is smudgy. Which is to say that as long as the photon energy is close to the resonance energy, there's a good probability that it will be absorbed. This is because of the uncertainty principle, which states puts a limit on the precision to which energy and time can be known simultaneously. Because transitions take a finite amount of time, there's also a finite wiggle room to their allowed energy values. This results in a finite linewidth to a spectroscopic transition, even one for an atom in a vacuum. We think of spectral lines from stars as "lines", but in fact they are very narrow bands. The width is defined by this smudginess.
Also, the resonance condition is a necessary but not sufficient criterion for absorption. There are a lot of other things that determine whether a photon is likely to be absorbed. So even if the photon has the right energy, it still may pass through unabsorbed. In Beer's Law the extinction coefficient (molar absorptivity) can be related via Avogadro's number to something called the absorption cross-section, which has a unit of area. It may be interpreted classically as a physical area through which a photon has to pass in order to be absorbed by the electron - kind of like a window if you will. The size of the area relates to the probability that a photon of a certain wavelength has of being absorbed by a single absorber (molecule, atom, whatever). The bigger the cross-section, the bigger the window, the higher probability of being absorbed. (The extinction coefficient, remember, has a unit of M-1 cm-1, or m2/Mol. So the molecular equivalent is m2.)