As far as etymology, homie is short for homeboy or homegirl. A homeboy or homegirl is a close friend with whom you have a lot of shared history or similar experience. They do not have to necessarily come from the same hometown or neighborhood. And the length of time of your familiarity is unimportant. Being around and sharing experiences with the same person everyday for the majority of the day will breed enough familiarity between each person for them to be homies in very little time, versus someone who you’ve known your entire life but share nothing or have nothing in common.
Conversely, two strangers could be homies if they are both from near identical backgrounds, upbringings, ideologies, affiliations, etc. You might address someone as homie if you wish to start a relationship based on those similarities. But, you would not introduce someone to a third party as your homie unless a level of trust between you and the person you are introducing has been previously established. –
As Andrew has pointed out, it can be used in a derogatory manner when used to mimic, mock or imitate a person from the culture or vernacular from which the term originated. Much in the same way any accent or dialect can be used to mock someone with that particular accent or vernacular. It can also be used in a threatening or aggressive manner. Much the same way as the word “friend” can be used with a threatening or aggressive tone to one’s voice when addressing someone who is clearly not your friend.