This is discussed in the Star Trek TNG Technical Manual (considered a canon source of info about the Star Trek universe).
In short, different engine configurations have been tried in the past but for ships over a certain size, a twin-warp-nacelle design seems to be the easiest way to achieve a well balanced warp field as well as offering the best opportunity to come out of warp in a controlled way in the event of a catastrophic failure of the flight systems:
Second, a pair of nacelles is employed to create two balanced,
interacting fields for vehicle maneuvers. In 2269, experimental work
with single nacelles and more than two nacelles yielded quick
confirmation that two was the optimum number for power generation and
vehicle control. Spacecraft maneuvers are performed by introducing
controlled timing differences in each set of warp coils, thereby
modifying the total warp field geometry and resultant ship heading.
Yaw motions (XZ plane) are most easily controlled in this manner.
During Saucer Module separation and independent operation of the Battle Section, interactive warp field controller software adjusts the field geometry to fit the altered spacecraft shape. In the event of accidental loss of one or both nacelles, the starship would linearly dissociate, due to the fact that different parts of the structure would be traveling at different warp factors.
Obviously we do see ships with multi-nacelle designs so this isn't a 'hard and fast' rule, but it does offer an in-universe glimpse at the rationale behind why we see this configuration so very often in both human and alien ships.
Out of universe, this was largely a design consideration based on Roddenberry's own prejudices. In a 2005 interview for Trekplace Andrew Probert (Concept Artist for Star Trek TOS and ST: The Motion Picture) gave them a solid overview of the design process as well as his own additions to the treknology canon:
Probert: Gene specified to me, in fact, that starship warp engines operate in pairs... only in pairs because they're codependent. If you had one warp engine, you'd probably go in a circle, I don't know... (laughs) So in the same breath he negated the three-engined dreadnoughts along with the single-engined destroyers, on the edict simply that, to achieve warp drive, you had to have codependent warp engine pairs. As far as the line-of-sight requirement, that was my edict, that, in order to be codependent, the warp engines had to "see" each other, totally. I'm taking about the power combs, not necessarily the Bussard collectors but the bulk of those combs have an energy path between them. And then for other starships, just like in World War II, where all the nations had fighter aircraft that all looked different -- you know, a cultural distinction between, say, a German aircraft and an American aircraft or a Japanese aircraft -- they all operated in the same way having the same basic components of wings, body, and engine, so I applied that thinking to the alien ships I designed as well, so the Ferengi ships, and Romulan Warbirds, have twin warp engines that have to see each other in order to operate. Even my shuttlecraft having a very shallow clearance, still see each other. That's why designs like the Romulan scout ship, where the engines cannot see each other, aren't consistent. There are also some cool Starfleet designs like the Nebula Class ships, but their warp engines cannot see each other. Even those runabouts ignore that ruling which messes up the continuity. Science fiction in particular NEEDS to be consistent. If you negate that,...it all falls apart.
h/t to @HorusKol for reminding me about this site.