There are two ways to get an oscillation going. The first way is to displace the oscillator from its equilibrium position and then release it. The oscillator will oscillate at its natural frequency. This is called a free oscillation. The second way is to exert a small but continuous external periodic driving force to the oscillator. In this case, the oscillator will be forced to oscillate at the frequency of the driving force. This is called a forced oscillation.
For a free oscillation, the energy is transferred to the oscillator in “one lump sum” at the beginning when the oscillator was displaced. The amplitude of oscillation starts off at a maximum and decreases gradually over time due to damping.
For a forced oscillation, however, energy is continuously transferred to the oscillator by the periodic driving force. The amplitude of the forced oscillation starts at zero, and increases over time, and stabilizes at a final amplitude when the rate of input of energy from the driver is matched by the rate of loss of energy to the surrounding due to damping.
The efficacy of energy transfer from the driver to the oscillator depends on how close the driving frequency is to the natural frequency of the oscillatory system. When there is a large mismatch, the energy transfer from the driver to the oscillator is inefficient, and the forced oscillation will only attain a small amplitude.
Resonance occurs when the driving frequency matches the natural frequency of the oscillator. At resonance, transfer of energy from the driver to the oscillator is at its most efficient, allowing for maximum amplitude to be attained.
Many teachers are very fond of the swing analogy, that you must push the swing at the correct timing if you want the swing to go higher and higher. Obviously, if you push the swing when it is coming back towards you, you’re being destructive to the amplitude building process. So in a similar (but not exact) manner, to achieve resonance the periodic driving force must be “synchronized” to the natural frequency of the oscillator, to ensure only positive (and no negative) work is done all the time to the oscillator.