If someone does something 'by the skin of their teeth', it means they just barely managed to do it. What is this idiom supposed to be referring to exactly, and how did it originate?
Because (of course) your teeth don't have skin, the expression
by the skin of your teeth
suggests 'by the smallest possible margin'.
This reference claims an origin in The Geneva Bible 1560.
The origin is a quote from the Bible. Job, a pious man, was tested by the god. He lost family, friends, money and health. At the end, he still kept the faith. He escaped, but remained with nothing. In this sense, he escaped with "the skin of his teeth", since the teeth do not have skin.
(source consulted: Carnal knowledge, C.Hodgson )