Hangnails are usually caused by picking the skin surrounded by the sidewalls of the nail, picking the cuticle or even biting the nail itself.
Hangnails are painful because the hangnail is still usually attached to the portion of the skin or nail bed that is still alive which is why it causes pain.
You can stop a hangnail from hurting by soaking the infected area in warm water once or twice per day for 20 minutes and then after you soak it in the water cut the hangnail off and then apply some Vitamin E oil to the affected area which will prevent another hangnail.
After you've done that apply some topical antibiotic ointment or cream on the affected area for a couple of days.