I have noticed that certain, seemingly random, words tend to sometimes have "ie" or "ei" in them. For example, the word "Foreign" has an "e", followed by an "i", but the word "friend", has an "i", followed by an "e". Why is this, and does one structure have a particular etymology that is different from another?
Foreign is from Old French and entered the English language in the 1300s. At this time it was spelled (most of the time) 'ferren, foran, foreyne'. The spelling was altered in the 17th century most likely to match other French origin words like 'sovereign' and 'reign'.
Friend is from Old English 'freond'. Its pronunciation was probably influenced a lot by the Old Norse presence in the East in the 9th century when a lot of sounds were broadened (ON friend 'frændi').
If you listen carefully, foreign and friend don't have the same 'e' when they are pronounced. The first one is lower and closer to a schwa. The second one is broader and closer to the 'e' in 'bed, 'met', or 'bled' (which are all from Old English).
Although not 100% consistent, the spelling of words can be understood from which language and what time the words entered the English language and whether or not they were revised at some point (such as Samuel Johnson), who was probably responsible for the current spelling of 'foreign'.
Most words with the short 'ie' variation come from Old English (thief, lief). Words with the long 'ie' sound (grieve). Note also that 'sieve', which has a short sound when its pronounced, comes from Old English.
Most words with the 'ei' spelling are a result of 17th century revisions to try and distinguish between OE and OF origin words. But Johnson and contemporaries missed quite a few so it's really all over the place.
Years (and years) ago everbody learnt the mnemonic rhyme
'i' before'e', except after 'c';
unless its 'e:' like beige, or sleigh.
That was helpful to those of us who couldn't detect 'the height of a beige ceiling.'
"Foreign" has superfluous letters, i and g, in "friend" the letter i is superfluous. There is no system in the English orthography. It is a compromise between two aims: to indicate the pronunciation and to give an idea about the historical origin of the word.
So in most cases etymonline (Eol), an online dictionary of etymologie, often gives an idea about the particular spelling of a word.
foreign: Eol gives Old French forain as immediate source. Later the spelling was changed. Eol: perhaps influence of reign.
friend: as Eol shows most cognates, related words in neighbouring languages, have the letter i.