Let’s start with the basics. The structure of an atom. An atom is the smallest constituent of any element, like gold, or oxygen, or carbon. It’s the smallest piece that you can cut a silver leaf into, so that it is still silver. If you cut it into still smaller pieces, it would split up into protons, neutrons and electron, and it would no longer be silver.
An atom has a certain number of protons and neutrons clustered together in its centre, and a number of electrons orbiting around it. You can picture it like the planets orbiting around the sun. The protons have what we call a positive charge, and the electrons have a negative charge on them. The number of protons in an atom equals the number of electrons in it, so that the charges balance out and the whole unit which we call an atom has no charge on it.
The number of protons, electrons and neutrons in an atom determine whether the atom is a carbon atom, an iodine atom, or any other element.
Electrons, if provided with the right amount of energy, can break free from their orbits and move away from the atom, leaving it with a net positive charge on it. This phenomenon lies at the base of why atoms combine to form molecules, and how electrolysis works to give batteries their charge.
So when you rub a plastic ruler against your hair, some of the electrons in your hair move to the ruler. Now if you bring the ruler near a few small pieces of paper lying on a table, the ruler attracts the paper. Why? Because a charged object attracts an oppositely charged object or a neutral object, so that it can neutralize or reduce its charge.
Static electricity explains why your hair sometimes stands on end when you comb it, or when you take off a hat. The strands of your hair all have the same charge, so they repel each other. This would not happen if your hair was wet or oily, or if the air around you was very humid. The water helps neutralize the charge using the surrounding air more quickly. Your comb would not attract bits of paper, and your hair would stay put. That is the basic principle behind preventing static charge. Provide a conducting medium that helps neutralize charges more quickly. This is how dryer sheets work to prevent lint from clinging to clothes.