The world is made of charged particles. Electrons have a negative charge, and the nuclei of atoms have positive charges. The amount of positive charge on any object is very nearly equal to the amount of negative charge, because the charges interact very strongly. The statements we have made about the behavior of currents -- that they stay in the wires, and that the amount leaving one end of a battery is exactly equal to the amount arriving at the other end -- are based on the idea that there never is any excess of charge of one kind or the other. A wire consists of nonmoving positive ions and mobile electrons, and when a current is flowing, the electrons run past the ions without ever disturbing the equilibrium. It's similar to the piping in your house, which always contains the same amount of water, even as it delivers water from the water company to you.
However, this is not always true. When two different materials touch, a few electrons will move from one to the other, giving a very local charge imbalance, and when the materials are then moved apart, a few electrons will "miss the boat" and be left behind. If the material does not conduct electricity, the extra charge will stay on the surface (and of course the other object has a corresponding positive charge).
The amount of charge involved is very small. The miniature lightning storm that results when you try to separate wool and nylon socks involves one billionth of the amount of charge that moves through a lamp cord in one second. But because the interaction between separated charges is so large, the amount of energy involved is relatively much larger; this implies that the voltages are very big indeed. It takes about 10,000 V to cause a spark to jump 1 cm in dry air -- and yet this happens readily in some circumstances. Despite the giant voltages, the sock lightning is not dangerous, because there is very little charge involved and thus very little current.
Static electricity can be a nuisance, though, if it means you are going to get a shock every time you touch a door knob. The real cause is that your shoes are a different material from the rug. The possible ways to cure this are
Why study sources of electrical energy?
The section on the sources of electrical
energy