When you rub two dissimilar materials together, it's possible for charges to get pulled off of the surface of one material and onto the surface of the other. This is the reason you get "static shocks" like what you experienced.
How much charge gets transferred and which way it moves depends in a complicated way on exactly what the two materials are made from and also on things like how dry the air is that surrounds the objects being rubbed.
There's practically no way to predict in advance which materials (cotton, wool, silk, nylon, etc.) you rub against other materials (amber, hard rubber, glass, plastic, etc.) will result in charge transfer from one to the other.
For more detailed information, I'd take user1936752's advice and look up triboelectricity.