https://en.wikipedia.org/wiki/Engine_configuration#Piston_engines for general coverage.
Piston engines have mass that goes back and forth, hence the term reciprocating engines. Moving it back and forth generates reaciton forces. Engines for vehicles need to fit inside. An inline 6 is [naturally balanced](https://en.wikipedia.org/wiki/Straight-six_engine#Engine_balance_and_vibration) so runs very smoothly. But it is very long. A V6 needs balance weights to run as smoothly but is much shorter at the expense of being wider. Cars are fine with this squareness.
Horizontally opposed 4-cylinder engines are shorter in height than inline 4, so they can go in situations that where height is important, like light aircraft or rear/mid-rear engine cars like the VW Beetle.
Another aspect is maintenance. If valves and spark plugs are accessible all from the same side it can be easier in a road car.
Generally, larger displacement means more air moving through the engine and thus more fuel burning, for more power.
Like everything in engineering, there will always be tradeoffs in design priorities.
Engineering Explained https://www.youtube.com/@EngineeringExplained is a pretty popular YouTube channel focused on the engineering of cars. I will leave it to you as an exercise to find engine design videos.
Latest Answers