Most words with 'g' read as 'g'
But sometimes 'g' reads as 'j'
And in the middle of a sentence, compare:
In short, because the English language has taken words from many other languages over the years, and with that borrowing of words comes the pronunciation that would be associated with that language.
During the Middle English period, we borrowed a lot from French, which used 'g' for a hard g before back vowels (a, o, u) and a soft g before front vowels (i, e). We started to use 'g' in the French way, while another letter (the insular g) took over spelling for the other sounds, before they disappeared or turned into other things.
So we have two g's because French had two g's. The rules governing wich pronunciation to use gets somewhat complicated though, because we've also borrowed 'g' words from other languages, but some guidelines to follow are:
Hard g before a consonant (glad, great)
Hard g before a back vowel (go, garden, gum)
Hard g at the end of a word (big, frog, leg)
Hard g if it's a Hebrew name (Gideon, Giliad)
Hard g before a front vowel in most words of Germanic origin (gift, get, gild)
Soft g for a word of Greek origin that starts with gy- (gymnasium, gymnastics, gyroscope). However! There is an exception to this rule for 'gynecology' and other 'gyn-' words.
Soft g before a front vowel if the word has a Romance origin (geography, giant, ginger, general)