If milk goes bad if not refrigerated, why does it not go bad inside the cow?

Milk goes bad inside the cow also. Milk is produced in mammals, as they have mammary glands. It will be in a sterile environment when the milk is inside the cow. Even in cow's body, milk should be taken out after some time because unless you do that, further milk will not be produced.

The cow may also get infected with bacteria through its teats if the milk is left for a long time inside. The udder may get inflammation and become painful for the cow. The cow's body will fight this infection through its immune system.

If the cow is not milked regularly, the mammary glands get reduced in size and stop further milk production and eventually gets dried off.