It will depend on the size of the cavity. Small cavities can be filled. Larger ones require root canal treatment. If the tooth is compeletly destroyed by a cavity than it needs to be extracted.
It is best to get the treatment done before the tooth hurts.