The short answer is, God picks them.
The long answer is, when a bishop dies or retires, a list of good candidates is prepared by somebody close to that situation, along with a list of what that place needs (Usually what they call a Papal Nuncio, essentially an ambassador. So The U.S. has a Papal Nuncio, as does Canada, etc.) That list is then sent to a commitee in the Vatican, who look at the needs of the place, and the backgrounds of the suggested people, and choose one person. That suggestion is then given to the Pope, who can either okay their pick, or choose any confirmed male catholic he thinks is a good choice (generally, he just okays the pick). That person is then notified, and will be ordained a Bishop.
Then for positions that are higher than bishop, they are chosen by the Pope, similarly, based on their merits and the needs of the position. A Bishop with a background in Canon Law is likely to be chosen for the judicial positions, while one with a background in theology will be chosen for a position which involves deciding doctrine.
The Pope is elected by the college of Cardinals (who were chosen by previous Popes). It essentially a majority vote. There is no (at least official) campaigning. People certainly don’t run for the office, although others may try and convince each other to vote a particular person. Again it could be anybody, but they usually elect a Cardinal (because they are experienced with how the whole system works)
The reason why some advance and others don’t can be attributed in part to shear luck. Maybe as a priest you were “lucky” and were assigned to a special position which got you name out there (rector/president of a Seminary, on a special advisory board, etc.) which got you recognized by the Nuncio. Maybe you were “unlucky” and just worked at parishes, while this is less likely to get you known around the country, this is where most bishops come from, Nuncio’s often ask Bishops if they have/know any good priests.
It is certainly nothing to feel bad about if you remain a priest your whole life, I once asked a Bishop what he missed most about being just a parish priest. His answer, “spending time with the people”. Being Bishop is a lot of paperwork and advising, and not a lot of personal time spent with the people. My Bishop is looking forward to his retirement later this year, so that he can return to Peru where he was a missionary for many years before becoming a Bishop.