Storage, temperature control, and airflow.
The storage part is the big one, imo. You keep animals in a barn, you don't want to fuck around going to another building for their food, cleaning supplies, etc. It's also more efficient land space wise. Building one or two levels up uses less land, which can then hold pastures, a different barn, whatever.
But it really depends on the type of barn. There's multiple types that gained popularity in the states over time. The oldest styles were English, with simple construction, lots of space for animals, with the storage above, but with usually only two levels. Here in the Appalachians, you see a lot of crib barns, which tend to be shorter on average, and are more for grain storage than animals. There's tobacco barns all over the south that are one or two stories, but the stories are shorter than the ones meant to hold animals and hay.
There's a kind of barn that's specifically built on hills so they have a basement/cellar.
The kind of barns for different types of livestock vary from the ones solely for crop storage as well as each other.
But the reality tall ones are almost always animal barns because you need the height more. You can store grain or whatever on a single level, and just add a little extra height for whatever airflow you need without having an entire extra story.
But when you've got livestock, especially cattle, the storage in the same building becomes mandatory, as does having a lot of extra height for good airflow. You get manure with livestock, and if you don't have good air moving around you end up with sick animals, no matter how well you clean up. There's modern methods that can sorta bypass the manure issues, but they have their own problems.
Even smaller animals like chickens, where you won't necessarily have a second story in full, you'll have a higher roof just for air to move. That's also why the old style longhouse barns work so well. You get a long building with doors at each end, high peaked roofs, with hay storage above. The air moves freely, which cuts down spoilage of fodder, eliminates mildew from built up moisture, keeps the animals healthy (it even reduces disease spread in some cases), and you get the ability to drop hay down instead of having to haul it around.
A three story barn isn't common afaik. It's usually two stories, with a high peaked roof. It ends up being three stories high, but there's only two levels. Some do have an attic built into them, but it's pretty rare since it reduces the benefits. But even that depends on where you are and the type of farm you're on.