There isn't always logic behind each choice of prepositions. But "in" is used for enclosed things. The farm is thought of as the land, which things are on. The cows might be in the barn, but they are on the farm.
If you have a question about the English language and would like to ask one of our many English teachers and language experts, please click the button below to let us know: