Writing a song is different depending on the genre. Even the word song means something different to different people. In hip-hop, there''s very little melody; mostly just rhyming. Country is different than pop and different than metal, etc. I''ll ignore hip-hop for this discussion.
When I write a song, I usually start with a chord structure to give me the feel of the song. Along with tempo and time signature, the chords tell us whether it''s a slow sad song or a fast aggressive song.
Next I decide on a theme. Is it "my wife left me and took the dog" or "watching the girls go by" or something else. No matter the theme, that should give a starting point. In it''s essence, the song should be a story. Not all songs are. Sometimes there just a melody with something catchy for words.
When you''ve got some words and chords, try playing and singing the song to yourself. It doesn''t matter whether you play the guitar, piano, or some other keyboard. You obviously can''t sing and lay if it''s a tuba! But you get the gist of what I''m saying.
When you play and sing the song, you''ll typically discover that some of the words don''t fit as well as they could with the music. But, you can adjust things from there.
If possible, record it. Even if on a mobile phone, that''s OK. Play it back and see how it sounds. Does it flow? Does it have a hook? Does it create an earworm? Does it make you want to tap you feet to the beat?
A good song should flow easily and grab some part of you. And, don''t forget that most songs start out rocky. There''s a work in progress where each iteration makes it better.