From A to Z
G stands for two main sounds: "hard" guttural g and "soft" gentle j the latter brought by the Norman invaders of 1066. G is soft when followed by e, i, or y as in pigeon, magic and Egypt. In more recent borrowing from the French, like entourage, g is an even softer zh sound.
Some Old English words resisted the Norman pressure (we hang on to a guttural pronunciation in give, get and girl), and g also stays hard in borrowings such as geyser from the Icelandic and gecko from the Malay. Other ways to keep g hard are doubling it (which also affects the preceding vowel sound, as in ragging as opposed to raging) or inserting a u as in guest, guilty and guy.
Gu, however, is not always associated with the hardsoft rule. In some words (such as guano from Spanish, languid from Latin and penguin from Welsh) it gives a gw sound. In others, like guard, guarantee and languor, it's just there (and in gauge, incidentally, it isn't).
G as a silent letter is even more fun. Initial silent gs come from all over the place - Ancient Greek (gnomic), Latin (gnome), Old English (gnash), German (gneiss). Delightfully, but not silently, there's also a gn word from the Hottentot language - gnu. Silent gs inside words are mostly Old English (igh and ough words) or Latin, as in sign and reign (in related words, like signature and interregnum, the g regains its voice).
The silent g in foreign, though, is a special treat - its Latin root is foraneus (an outsider), so the g is merely a lexicographer's whim, based on false analogy with reign. Gorgeous!