This is a difficult thing to teach people because as one of the other responses mentioned, it's just a thing you become able to do after you spend a lot of time playing in an ensemble. But I think I might at least be able to tell you kinda what is going on in my head and hands when I do it. BTW apologies in advance for the length, but this is kind of a deep subject.
When I'm playing bass, what my head is doing is thinking about the sound of the next chord. In my head, I can hear and sing the root (or sometimes whatever is voiced as the bass note), and I can hear enough of it in my head to know a few details, like whether its major or minor, whether it has minor 7 or flat 5. There are obviously oodles of other chord qualities, but if you are aware of at least those details of the chords in the chart, it'll take you pretty far as a bassist.
For the moment though, lets put aside everything but the root. Most of the time when I'm playing, unless I'm actually looking at the chart, I am not consciously aware of the note that I'm playing. I could look down at the neck and tell you what it was, but there is no conscious process of thinking "next, I shall play the Db" and then doing it. Instead, I hear the note that I am playing right now, and I imagine how far away the next root is. This is much easier than it sounds- for example, pretty much anybody can join in on a rendition of "Happy Birthday" even if they have no idea what the starting note was, and it's pretty much always completely random.
Once you can imagine the distance between the pitch you're playing and the pitch you're anticipating, you need to translate that into motion. Again, sometimes people think that this is impossible, but if you can sing happy birthday in a random key in tune, which requires very precise modulation of your vocal chords, you can do it on any other instrument too if you practice enough. In common music, the number of potential intervals between successive chords is not really that large, and especially on a bass there are many tunes where you can play every chord root in a tune with very little shifting in the left hand.
So the real secret to it is, if you are fretting some note with, say, the third finger of your left hand, and you can hear the next root in your head because you've listened to the song a few times and you remember it, what you need to develop through practice is a map in your hands and brain about where the potential next-notes are located. Many styles of music feature common root motion, like the blues, which means as a bassist once you learn where the I, IV, and V are, if you can find the tonic on the fingerboard using your brain and eyes, your fingers can find the IV and V for you. Jazz is the same way except there are way more variations of root motion.
The part that I skipped, the chord-quality stuff, takes longer to develop. It will give you the ability to imagine next-notes that aren't roots, but are related to the chord and help you to take a path which leads you to the next root, rather than just going straight to it. It's how players who don't know a lick of theory can still play incredible walking lines; it turns out they know the harmony but maybe just don't know the names that people have given to aspects of harmony.
Anyway once you've started to develop that intuition about finding next notes, then the key you're playing the song in really doesn't matter. Some keys are harder to play bass in than others, because they can make it difficult to take advantage of open strings, or require a lot of shifting, but most of the time it's just a matter of finding the new tonic and then working out a comfortable place on the neck to home yourself.
I learned this skill by spending decades playing along with records, using trial and error to find each next note. Sometimes it was at a piano, sometimes with a horn, later with the bass, often just sitting playing along with jingles on TV commercials, sometimes harder stuff that required a boatload of rewinding. Today with looping software its way easier to pick phrases, loop them in the headphones, and to just try and fail over and over until you can mimic the line on your instrument. Today I will sometimes write out the notation for stuff I transcribe, but often I don't and I have no idea what the notes are until I do- I just figure out _how_ to play it first and then figure out _what_ I played after.
Anyway apologies for the length, this is kind of a heavy topic I've been meaning to write something about for years, and your question was not one I'd seen before but I realized that this whole playing-by-ear thing is pretty critical for that and not something that ear-players often explain in an understandable way, so hope this helps some.