Specifically, it's found in Job 19:20:
> "My bone cleaveth to my skin and to my flesh, and I am escaped with the skin of my teeth."
In this context, Job is lamenting his suffering and hardship. He's saying that he's barely escaped death, having only the skin left on his teeth.
Over time, the phrase evolved to mean narrowly escaping a difficult situation, often with little to spare.