The easiest way to write a melody is to come up with the chord progression first and write the melody to fit that chord progression. There are a lot of songs out there that only use three chords: the 1 chord, the 4 chord, and the 5 chord. These three chords can be combined in innumerable ways. Here are a few things to keep in mind: the chord progression typically starts on the 1 chord and goes to the 5 chord before resolving back to the 1 chord; the 5 chord creates tension: it makes the listener want to return to the 1 chord. A typical chord progression might look like this: 1, 4, 1, 5. The chord progression for a blues song looks like this: 1, 4, 1, 5, 4, 1, 5. (Remember, you don’t have to stay on each chord in a progression for the same length of time.) Here’s the chord progression for "Twinkle, Twinkle, Little Star" (the chord numbers are in parentheses): (1) Twinkle, twinkle, (4) little (1) star, (4) how I (1) wonder (5) what you (1) are.
In addition to the 1, 4, and 5 chords, you can also use the other chords in the key (2, 3, and 6, all of which are minor chords). Minor chords tend to sound sadder than major chords, and songs in minor keys (centered around the 6 chord) tend to have melancholy lyrics. You can also experiment with using chords that are not in the same key—a flat 7 major chord (a major chord a half step lower than the 7 note in the major scale) is a popular example. For instance, in the key of G major, an F major chord would be a flat 7 major chord.
The best way to come up with a chord progression is by trial and error. You can also use the chord progression from another song (as long as the melody is different). It might be easier for you to write the lyrics first and then write the chord progression. However you decide to do it, though, there should be one chord progression for the verse, one for the chorus, and another for the bridge. The chord progression for the verse establishes the key and therefore usually starts on the 1 chord. The chord progression for the chorus often begins on the 4 chord. The chord progression for the bridge often begins on the 6 chord (which, as you know, is minor). These are only generalizations. No chord progression is absolutely wrong.
Now that you have your chord progression, it’s time to write the melody. The piano is an ideal instrument for writing a melody—all the notes are laid out and organized for you. To make things easier, you might want to transpose your song to the key of C for the moment (which you can easily do, since you know the numbers that correspond to the chords). The reason for this is that, on a piano, the white keys are the C major scale. Have a friend play your chord progression or record it on a tape recorder and play it back. As your chord progression is being played over and over again, just start playing around on those white keys. There are some general rules I could give you for writing a melody—such as, if you skip notes on the scale, do it in thirds or fifths, and after a jump like that, switch directions—but these rules can be and are broken more than any other rules in music. A melody is very subjective. The ultimate test is whether it sounds good to you.
Another way to write a melody is to play your chord progression and hum or sing along, trying out ideas. If you already have lyrics, try singing them in whatever way comes to mind. The chord progression will often suggest a melody.