The simple answer is that
a bike stays upright when rolling forward if it is steered in the direction of any lean that might develop,
and a simplistic explanation for why this works is that it moves the ground support back under the center of mass. A more nuanced explanation that allows for the lean angle that bikes maintain in a curve is that the tendency to rotate in the direction of a lean caused by gravity pulling down on the center of mass is countered by a tendency to rotate in the opposite direction caused by friction of the tires on pavement pushing them in the direction of the lean.
The steering torque necessary to make this happen is usually provided by a rider, but many bikes, if rolling forward at the right speed, can create the necessary steering torque automatically.
Researchers know enough of how human riders create the necessary torque to create bicycle riding robots, but they are still teasing apart exactly what riders sense; is it lean angle, lean rate, steering angle, or some combination; how they sense it; is it visually, with their inner ears, kinesthetically, or some combination of senses; and exactly how they respond; do they push on the handlebars, lean their torso, or some combination of motions.
At the same time, researchers have a pretty good handle on how a particular bicycle can create the necessary steer torque by itself. There are four main effects that can generate torques about the steering axis: gyroscopic precession of the spinning front wheel, ground reaction forces acting through geometric trail, location and distribution of mass of the front assembly and rear frame, and tire properties. Two of these, precession and trail, have been shown to be neither necessary nor sufficient by themselves. On most real, well-designed bicycles, however, all four effects combine in just the right ratios to steer the front wheel just the right amount.
There is a set of equations, based on an idealized model of a bicycle developed in 1899 by English mathematician Francis Whipple at Cambridge University, that can predict if and at what speed any particular bicycle will exhibit self-stability. These equations incorporate the gyroscopic effect, trail, and mass distribution, but ignore tire properties. Never-the-less their accuracy has been confirmed by at least two separate experiments.
First, they accurately predict the frequency at which a standard, upright bicycle will weave back and forth when rolling forward without a rider. Second, they predict the existence and dimensions of a self-stable bicycle with no gyroscopic effects and no trail. Such a bicycle, called the “two-mass skate” bicycle, was built by researchers at the Technical University of Delft, in The Netherlands, and videoed rolling across a gym floor on its own and even rejecting a sideways nudge as it did so.
The equations do not, however, give a clean accounting for which factor contributes how much to self-stability, which some people are willing to interpret as “science can’t explain why a bicycle stays up.” I believe that this is an unfair characterization that might make attention grabbing headlines, but that misrepresents the situation. For example, there is no simple equation to calculate the lift coefficient exactly for a given airfoil. Instead, it has to be numerically simulated or measured empirically in a wind tunnel, but no one is saying that “science can’t explain why an airplane flies.”