NMR is based on the principle that nuclei of certain isotopes have an intrinsic magnetic moment. When placed in a strong magnetic field, these nuclei align with the field. By applying a radiofrequency pulse, the alignment is disturbed, and as the nuclei return to their initial state, they emit signals. These NMR signals are detected and transformed into spectra, which are used to infer the molecular structure of the sample.