How do head rest on a car seats work by using laws of physic?
Headrests don't work. Headrests don't move. If they don't move,
they don't perform work. That is a law of physics. Work is defined
as "Force through a distance".
...but....
You probably are asking how they function relative to the laws
of motion. The headrest prevents your head from continuing in its
current state of motion when the car is struck from behind. Without
the headrest, your head snaps backward relative to the seat and
strains the neck. That sort of injury is called "whiplash".