I suspect that homographs and homophones may arise as multiple words from different languages are brought into a language, winding up with the same graphemic and/or phonemic representation.
The same goes for synonyms; different languages imbue a language with two different words that both mean the same thing. In the beginning, there may be some "survival of the fittest" process that eliminates one word, as it is redundant. However, sociological structures may make one word in heavy use in one sociolect and another word in heavy use in another (I refer to Norman being spoken by the British upper-class and Old English being spoken by the lower-class).
Poetry surely played a role in sustaining the existence of impractical synonyms in a language. Certain words may be less known and longer and/or harder to read/pronounce, yet be more phonaesthetically appropriate for poetic application, and thus be favored in poetry and literary prose, despite a shorter, more well-known and practical synonym being used in common parlance.
In fact, I think these mechanisms make their way into common parlance as well. Due to the different phonaesthetic experiences words produce, they gain different connotations, which lowers their synonymy and thus, lowers their redundancy, prolonging their coexistence in a language.
I think there's also cultural/etymological factors behind connotation-sustained coexistence of synonyms. In English, one word (1) may be of English etymology, whereas a synonym (2) to that word comes from some other language. Due to having read English literature, and having read (1) in literary contexts, the word gains stronger connotations, whereas (2) has far less connotations. This also lowers synonymy, as (1) will mostly be used in alignment with those connotations, and (2) will be used to avoid connotations, eventually gaining connotations of its own.
Perhaps geo- or dialectical differences may make the same word sound/look different over time, and then some kind of process makes both of the geo-/dialectical versions a part of the standard language?
I believe there are probably more mechanisms behind this. Also, the mentioned mechanism are purely from my own speculation, so they may be wrong. What are the mechanism, both those known and theorized/hypothesized, behind the emergence of homographs, homophones and homosemes in languages?
EDIT:
Firstly, I want to make clear that I changed homonym to homograph, as I found out homonyms are homographic and/or homophonous, whereas homographs are simply words that share spelling.
Secondly, I want to add another way in which I suspect homonyms are added. Some linguists seem to regard the set of words that homographic and homophonous, somewhat synonymous and of common etymology, also known as senses of a word, to all constitute one word. I believe senses arise due to multitude of reasons; (a) ignorance, (b) ideological manipulation of a word, (c) word being used in different contexts and/or fields and (d) disagreement about the correct conceptualization of a word. Sometimes overlapping is (b) and (d), but not always.