An animal may be trapped outside a barn or coop if the doors are closed at night. The conditions that cause an animal to be trapped outside are not clear. It may be more likely for an animal to be trapped outside if the player does not leave the farm after 5pm (when animals begin to return to the barn/coop) and does not enter the barn/coop after 5pm. (Leaving the farm and entering the barn/coop after 5pm both seem to cause animals to transport or spawn to the inside of the barn/coop automatically).
If it rains the next day, and the animal was left out, the animal will stay outside in the rain. This is the only instance in which a farm animal will be outside during the rain.