Homonyms are words that are spelled the same and sound the same, but have different meanings. I know that may be different from what you thought. Many people think it simply means two words that sound the same, like time and thyme. These would, technically, be homophones, which may be spelled the same, or differently. There are further gradations from here, but it gets a little complicated.
So how do words end up being spelled the same and sounding the same, yet having different meanings? Usage, usually. How a word is used changes over the years (think of how “text” is now used as a verb), but often the root or origin of the words are the same. One of my favorite examples is the word reservation, which basically means a holding back. Restaurants can “hold back” a table for you, or you can “hold back” your approval of something by having reservations about it. Which leads us to the classic joke about the couple who arrived at the restaurant and were asked by the maître d’ if they had any reservations – to which they replied, “Oh, no. We’re sure we want to eat here.”