If you pass a test by just a few points or your team barely wins the soccer game, people might say you did so by the skin of your teeth. There was no room to spare, and you did it by a narrow margin.
Where did it come from? Your teeth sometimes have a film on them, especially first thing in the morning before you’ve had a chance to brush! This film could be considered “skin,” and it is very thin. If you did something by the skin of your teeth, it was just barely done.