When a photon comes, it actually couples the electron sitting in a certain level with other levels which are separated from the initial level by about the same amount of energy as the energy of the incoming photon. The result of this coupling may end up as an up transition (absorption where the photon get absorbed) or down transition (stimulated emission where the electron goes down to a lower level emitting another photon of the same energy). Whether the electron will undergo absorption or stimulated emission is determined by the transition probability between the initial level and the other level.
As for spontaneous emission, the proper treatment of this event can be done using quantum electrodynamics (QED). It has to do with zero point energy, when you go deeper in QED you will find that vacuum is not really nothing. By quantizing electromagnetic field, it has been found that the absence of photon doesn't mean the energy is zero, I would say it's almost like the case with quantum harmonic oscillator.