According to the Library of Congress, static electricity occurs when the surface charge of an object changes. If someone rubs certain types of materials together, electrons can jump from one to the other, changing the effective charge of both items. Positively charged items attract negatively charged items, while items with the same charge repel. Bringing two objects together with significantly different charges can result in a vivid spark.
The electrons of atoms are sometimes held by relatively weak electrical forces. Electric current, for instance, requires the transmission of electrons down a chain of linked atoms. When an atom loses an electron, its effective charge becomes positive, and if it gains an extra electron, it becomes negative.
For instance, rubbing a balloon on human hair transfers electrons from the person's hair to the balloon. The person's hair becomes attracted to the balloon, while the individual strands will repel each other and stick up.
If one walks across a rug in socks, the movement of fabric on fibers can attract electrons from the carpet. Enough movement can build up a significant negative charge, so that when one touches a metallic object, it conducts the extra electrons away from the skin. This is why it is important to discharge static electricity before working on a computer, as the electrical discharge can damage internal components.