This idiom first shows up in the 1680s, and at that time it has two forms:
A. the modern version know better than to, and
B. a fuller version know better things than to.
A and B trot along side-by-side for about a hundred years, but towards the end of the 18th century B drifts out of use.
The idiom is founded on long established expressions (they go back at least to Chaucer):
know [something] better than [something else], meaning to be aware of something which is better than something else
know [something] better than [someone else], meaning to be more knowledgeable about something than someone else is.
The B version obviously has sense 1: know things to do which are better than what you are doing or proposing to do. I would guess the A version to have been originally just a shorter version of the B version, with better understood as a nominal something better. But I suspect it finally superseded B because it also accommodated understanding in sense 2: you are too knowledgeable to do what you are doing or proposing to do