When hair stands up, it is due to a buildup of static electricity. Static electricity is created when two objects rub together and electrons are transferred from one object to the other. This creates an imbalance of charges, with one object having a positive charge and the other having a negative charge. When the charged objects are close together, the electrical forces between them cause the hair to stand up.
Static electricity can also be created by other means, such as when you walk across a carpet or when you take off a sweater. In these cases, the friction between the two objects creates a buildup of static electricity, which can cause your hair to stand up.