Leviticus 13:1 And the LORD spake unto Moses and Aaron, saying, 2 When a man shall have in the skin of his flesh a rising, a scab, or bright spot, and it be in the skin of his flesh like the plague of leprosy; then he shall be brought unto Aaron the priest, or unto one of his sons the priests: 3 And the priest shall look on the plague in the skin of the flesh: and when the hair in the plague is turned white, and the plague in sight be deeper than the skin of his flesh, it is a plague of leprosy: and the priest shall look on him, and pronounce him unclean.
God knew because He is God. He told people long before science knew. You serve a great God who has a great, inspired, Word for us.
Amazingly God tells the children of Israel about contagious diseases all the way back in the time of Moses. The Bible has to be divine for the amount of things that God gave to man before science could figure it out.