I can approach it two ways:
I know what "fits" -- I grasp the harmony, meter, etc. and can write something that will be functional. In an interview, Sting said something to the effect that "There are actually only about five bass lines." That's pretty pragmatic, but... it might be a good way to start thinking about it.
I listen to the song a few times without the bass. If I've listened enough, the melody will be in my head, and I can sort of organically come up with something musical, while I'm walking around or something. Often, I'll be inspired to reinforce the melody at key points by doubling it or harmonizing it, or I'll realize later that the main motif I hear for the bass is actually a keyboard part, say, from an entirely different song which was suggested to me by the one I'm working on. The other song might be something from a different genre, and decades old.
One of the fun things about being old, like me

is that you have so many years of source material stuffed away in your brain. (Yet, I can't remember to take out the garbage when my wife asks me to.)