Because it doesn't fit the pattern, that's why. Outward appearances are very deceiving.
It would be easy to make up a superficially convincing sham etymology for it. Like so: there are a number of words from the French that begin with "g-" but which in their English incarnations begin with "w-"; the name William is a good example, as the French version is "Guillaume", and also the word pair "warranty", an Anglicization of French "guarantie", which also became English "guarantee". The same thing happened with English "wall". So French "gallerie", which became English "gallery", is so named because it was originally a small porch or garden enclosed by walls rather than being open-air.
That last two sentences are false, of course. Well, almost. A gallery actually was originally a small porch, and we did get the word from Old French "galerie". But the word never had anything to do with walls. Instead, it comes from a most unlikely place; Galilee.
Not the city, though. A galilee, lower-case, was a porch or vestible leading to a church. In mediaeval Latin, it was "galilaea", which eventually became "galeria" (which it still is in Spanish, and in Italian "galleria"), from which French and subsequently English derived their words.
The sense of "gallery" as "building for exhibiting art" entered English in the late sixteenth century. It didn't just happen: "gallery" had already represented various sorts of rooms or parts of buildings, and still does; it can refer to, among other things, a narrow corridor or the cheapest seats in a theatre.
"Galley", in case you were wondering, seems to be unrelated, and simply sprang unbidden into Greek (as "galea") and then, predictably, through Latin and then French.