I don’t think it answers the OP’s question to suggest that the solution is to write music that conforms to a genre. The guides that @Alyen links to are a good starting point, and understanding the history of the genres – as suggested by @blakkaz – can also be very useful in helping you identify the genres of songs. But I don’t think it entirely answers the original question.
My tuppence-worth would be that it can be very hard, if not impossible, to pinpoint a genre for a track. If you happen to always write music that conforms to one or two genres then that’s great! You’ll have no trouble identifying your music’s genre. But if you prefer to be more experimental in your compositions then you might find it difficult or impossible to say with any certainty what your genre is.
Using a mainstream artist as an example, how would you classify the genre for this track (one of my favourite ever songs) from AIR?
I love this song so much, but I couldn’t say with any certainty what the genre is. In fact I’d struggle to even assign an overall genre to AIR as a band. Some of their stuff is pop, some of it chill, some of it experimental, some of it lively, some of it sad or depressing. I’ve seen all kinds of genres assigned to them, most of which I wouldn’t agree with.
My personal suggestion is to not lose too much sleep over it. If you think a song could pass as a given genre, then label it as such. If you can’t decide on a genre for a song, just give it a generic tag such as “Electronic”.