It comes from the word "βάρβαρος" (barbaros), which literally meant "foreigner" or "non-Greek speaker". The Greeks perceived other cultures and languages as being "barbaric" because they sounded like "bar-bar-bar" to them.
Over time, the meaning of "barbarian" evolved to carry a more negative connotation, referring to people considered uncivilized or savage.