By doing it a lot. Like anything, the more you do it the more you'll improve. Always start with the original bass line on the record. This isn't about you being original, it's about you understanding the relationship between notes. It's easier to start with an existing bass line that you can replay as many times as you need.
Listen carefully. Start with songs you like and know then move on. Notice how the notes follow each other. Notes are either higher, lower or the same as the last note. First you'll start to hear the direction of flow and the patterns(12 bar, pachelbel, 1,2,5 etc), then you'll start to hear the intervals, then you'll anticipate the notes and be playing on the hoof.