they differ mostly in nuance of meaning.
Grim can be used to describe a stoic sadness or ill-fated result
(e.g.) "the funeral attendees showed grim expressions." or "the results of the exam were grim"
Gloomy is used more to describe a depressing atmosphere or mood
(e.g.)"this dark room is so gloomy"
or "that person is always so gloomy, being around them makes me depressed"