Launch vehicle trajectory optimization is a complicated subject, and I can't say I fully understand it. But I can list some of the important considerations.
Atmospheric drag is a big consideration in first-stage flight from the earth's surface. You obviously want to reduce it by getting above the thickest parts of the atmosphere as soon as possible.
Gravity losses are another big consideration in first-stage flight. Except at a pitch angle of zero (i.e., the launch vehicle is horizontal) some of its thrust goes to counteracting gravity rather than accelerating the launch vehicle. Gravity losses are clearly a maximum at liftoff when the launcher is pointed straight up and is still full of unburned propellant. To minimize gravity losses you a) use the highest thrust-to-weight ratio you can afford (which is why large solid rocket boosters are so popular) and b) reduce the pitch angle as quickly as possible. Obviously you can't do this too quickly or the rocket will fall back onto the ground!
At liftoff the engines must support the rocket's entire weight as well as accelerate it to orbit, but as you accelerate downrange the earth will begin to fall away from you due to its curvature, leaving less of the rocket's weight to be supported by upward rocket thrust. (Once you're in orbit, then by definition no upward supporting thrust is needed, as the earth is now falling away from you at least as quickly as you free-fall toward it.)
I.e., you obviously want to build downrange velocity as quickly as you can, but this is also at odds with gaining altitude as quickly as possible to get out of the thicker air near the ground, especially before you build up much velocity.
This calls for the classic curved trajectory that begins by going straight up from the launch pad to gain altitude and then progressively pitching down toward the horizontal as you pick up velocity.
The aerodynamic forces on the rocket depend on both its velocity and the density of the surrounding air, so as the rocket accelerates into thinner air these forces reach a peak and then decrease to zero. This "Max-Q" peak usually occurs between 60 and 90 seconds after liftoff, and the forces can be so extreme that even a small non-zero angle of attack (the angle between the rocket's longitudinal axis and the relative wind) can produce a bending moment that tears the rocket apart. This is usually done with a maneuver called a "gravity turn", which lets gravity gradually bend the launch trajectory toward the horizontal while maintaining a near-zero angle of attack.
Controlling the angle of attack in the max-Q region is so critical that Apollo's Emergency Detection System automatically triggered an abort if the sensed angle of attack exceeded a safe threshold; waiting for the commander to do it manually could well be fatal.