Hello is younger than most people think, and it was not a greeting at first.
It is an alteration of hallo, which is an alteration of holla or hollo, a shout used to catch someone's attention. Etymonline traces the family to the late 1300s, when Middle English had a verb halouen, "to shout in the chase." The Oxford English Dictionary connects it to Old High German hala and hola, commands meaning "fetch," used especially to hail a ferryman across a river.
Mental Floss says holla came into English from French holà in the 1500s, meaning "stop." Variants such as hillo and hollo were used to hail someone far off or to shout at hunting dogs. It reports the first printed hello in the Norwich Courier of Connecticut in 1826, where it was a call for attention or a cry of surprise. Sources disagree on the exact day.
As a greeting it is a nineteenth-century development. Etymonline's earliest greeting uses, from 1848, come from the American western frontier.
Then the telephone arrived. Alexander Graham Bell wanted callers to answer with "Ahoy." In an August 1877 letter, Thomas Edison wrote that a call bell might be unnecessary, since "Hello!" could be heard ten to twenty feet away. The Oxford English Dictionary says he is popularly credited with popularizing the word as a phone greeting. Edison did not coin it. He backed an existing shout for a new job.
Hello won. The first phone book, printed in New Haven in 1878, suggested "a firm and cheery hulloa." By 1880 the women running telephone exchanges were being called "hello-girls." Etymonline says the word's popularity as a greeting grew through the 1880s alongside the phone.