It is due to the growth of microbes in it. If the milk is boiled well, and then sealed up in a vessel, it will not turn sour in any whether or in any length of time, because all the microbes in the milk, including those that turn it sour, have been killed by the boiling. Much humidity and electricity in the air to which milk is exposed favor the growth of microbes in the milk. Microbes are plants, and we know that humidity and electricity in the air favor the growth of plants.
The substance in sour milk that gives it sour taste is an acid, which is called lactic acid. This acid is produced by microbes from the sugar in the milk, which has the corresponding name of lactose. FYI: Cottage cheese is made from sour milk.