There are two physiological facts that need to be considered here:
Muscles consume energy not only to convert it into mechanical energy, but also to sustain a certain force. The greater the sustained force, the more energy is wasted into just sustaining the force.
Muscles cannot work indefinitely fast. There are two broad categories of muscle fibers which are distinguished by how fast they can twitch. The important fibers for biking are the slow twitch fibers because they burn their fuel with oxygen. The fast twitch fibers generally like to use anaerobic chemistry, and are thus not suited for endurance sports like biking. When the speed of the muscle contraction gets into vicinity of the fibers twitch speed, force output drops overproportionally, making the muscles inefficient.
As such, the perfect power output is achieved with a cadence that is as high as possible (less force = lower losses due to effect 1) without loosing too much force due to effect 2. That is the reason why professional athletes like to spin at around 90rpm.
Now, for ascents, you first use your gears to keep your cadence high until you run out of gears. At that point, your muscle efficiency will start to drop with your cadence. You will waste more and more energy into putting enough pressure onto your pedals via your loaded muscles. Until this wasted effort becomes so great that it forces you out of saddle. The point is, when you ride out-of-saddle, you can use your muscles to lift your body at the start of a stroke, straightening your knee much earlier than when in saddle. Once your knee is straight, your muscles can relax and let gravity take over pushing the pedal further down. This significantly reduces your energy losses due to effect 1.
Now, getting back to your biking: If you know your power output on the flat and the lowest speed at which you can maintain your cadence by switching gears, you can derive the grade on which you can still ride that speed. As an example, assume 200 W power output, min speed of 10 km/h, and a cyclist/bike weight of 100 kg. This rider can sustain a vertical speed of 200 W / 1 kN = 0.2 m/s, which translates to a grade of 0.2 m/s / (10 km/h) = 7.2%. This will be close to the most energy efficient climbing speed, and you will be able to endure that as long as you can ride on the flats. Any stronger ascent will reduce your climbing performance up to the point where you have to give up and give in to pushing your bike uphill.