Mothballs contain the compound 1,4-dichlorobenzene. The direct contact or vapors from this compound are toxic to insects and their lavae, thus preventing infestation and them susequently eating your stroed clothes.
It is a chemical connection which evaporates easily, and which are very unpleasant for the moths. In former days mothballs existed from camphor, but nowadays they use paradichloorbenzeen.