Capping agents work by attaching to the surface of nanoparticles through various interactions such as covalent bonds, ionic bonds, and van der Waals forces. They form a protective layer around the nanoparticles, which can either be hydrophobic or hydrophilic, depending on the nature of the capping agent. This layer prevents the nanoparticles from coming into close contact with each other, thereby preventing aggregation.