Nanofiltration membranes work by allowing certain molecules to pass through while blocking others, based on their size and charge. The process involves the application of pressure to push a solution through the membrane. The smaller molecules and ions pass through the membrane, while larger molecules and multivalent ions are retained. This selective separation is achieved through a combination of sieving and electrostatic interactions.