The words "grim" and "gloomy" are somewhat vague, so there isn't an exact distinction between them that most speakers would understand. As usual, the best way to understand these subtleties is to take note of their primary meanings (which are sometimes rare or partly forgotten), and follow how people reasonably extend those meanings to describe different situations.
The noun "gloom" primarily means the dull or dark lighting that you find at twilight, on a very cloudy day, or within a shadow—with the connotation of the melancholy or despondent emotion that people often feel in places with this kind of lighting.
Here is a very gloomy place:
People often extend the primary meaning to describe anything related to that emotion, even if doesn't necessarily involve dim light, though usually the concept of dim light "colors" the description. For example, the poet Langston Hughes wrote these words to describe the closing of the Newport Jazz Festival in 1960:
It’s a gloomy day at Newport. It's a gloomy, gloomy day. The music’s going away. (Source.)
Notice that the noun modified by "gloomy" is "day"; and the day is called gloomy to express the sad and discouraged emotion resulting from the loss of the festival.
The adjective "grim" describes a mental attitude of determination to do a cruel or harsh deed, usually serving a very serious purpose, such as executing a convicted murderer. Secondarily, it suggests the facial expression of a person who has resolved to carry out such a deed and will not be stopped from doing it. For example, death is often personified as "the grim reaper", usually depicted like this:
So, when call a place gloomy, you suggest dim lighting and/or a melancholy mood. When you call a place grim, you suggest that it's used for grave purposes and/or gruesome deeds. The sorts of places you would most easily call grim are gallows, execution chambers, slaughterhouses, prisons, battlefields (especially just after the battle), mortuaries, morgues. People often imagine that grim places are also gloomy; for example, in a painting or movie, you're likely to see a corpse-strewn battlefield depicted under a dark sky. So, there's naturally some overlap in how people use and understand these words.
Source for the gloomy image.
Source for the image of the grim reaper.