In rocket terms, maximum exhaust velocity is mainly a function of the fuel/oxidizer combination and chamber pressure, and produces a figure of merit called "specific impulse." This is directly proportional to exhaust velocity.
Note that nozzle input pressure (chamber pressure, in a rocket) is a major contributor here. A Merlin engine (used in the Falcon 9) and an F-1 (used in the Saturn V booster stage from the Apollo program) use the same fuel (highly refined kerosene) and oxidizer (liquid oxygen) but have significantly different specific impulse, because the Merlin operates at much higher chamber pressure.
Generally, overexpansion is the thing you want to avoid in nozzle design; this causes pressure zones and standing shock waves inside the nozzle bell that can damage the structure and result in an engine failure. Underexpansion just means the exhaust has some level of unextracted energy when it leaves the nozzle. Underexpansion is very common in real rockets; Falcon 9 launches exhibit it quite visibly (in the form of the very wide exhaust plume) for the last minute or so of the first stage burn; this is the case with all orbital rockets launched from the ground, because the engines must be designed to work well at sea level pressure.
Significant overexpansion is seldom seen in practice -- but a recent example was when SpaceX Starship 20 test fired the one Vacuum Raptor engine that was installed at that time, just a few days ago, with the vehicle clamped down on the test stand at sea level. More commonly, engines are designed with a compromise expansion value allowing safe operation at sea level and less underexpansion at maximum operating altitude (typically around 80-100 km -- effectively vacuum -- for a booster).