The original question is equivalent to "how to compose music." It's a lifetime study. However, if you want a nice melody to go with a given bass line, there are a few procedures (not rules, just what has been found by others to work well.)
The first idea is to avoid parallel perfect intervals. When two lines of music move by the same interval, the lines often sound more like a "rich" single melody, rather than two independent lines. The rule is often stated: "avoid moving to perfect unisons, octaves, or fifths by parallel motion.) The idea is to have the bass line support the melody, not merely reinforce it. If a pair of lines move a few steps (even 2, but 3 are easy to hear), the whole composition sounds a bit like one instrument dropped out. This rule also helps improvise a bass line when accompanying a jam session.
It helps vastly to know a bit about chord progressions, chord inversions, and a bit of other stuff. This one can learn by trial and error or (much) faster by formal study.
Another useful procedure when just stuffing things above a bass line (which is not a bad way to compose a piece.) It's called the "Rule of the Octave" or more commonly "Regle d'Otava"). It was devised for Renaissance and Baroque keyboard players to improvise chords above a bass line. The idea is to suggest chords that work well above certain bass notes in rising or falling bass lines. This is a bit more complicated than just avoiding parallels.
https://www.partimenti.org/partimenti/about_parti/rule_of_the_octave.pdf
http://www.singanewsong.org/byu/theory/grad_review/The%20Rule%20of%20the%20Octave.pdf
http://musictheory.sites.gettysburg.edu/unit-4-1/the-rule-of-the-octave/
http://musictheory.sites.gettysburg.edu/unit-4-1/rule-of-the-octave-in-minor-keys/
There's more. Once you find a few things that work, you will find more; it becomes addictive.