There are several different theories to the answer to your question.
Below you will find a few of them.
According to the United Stated Department of the Army:
"The salute is an act of recognition between military men (and women). Its origin is in the ancient European custom of free men greeting each other by holding up their right hand to show that they had no arms. Prisoners do not salute; they are denied the privaledge."
According to CLT Sevigny
"The hand salute probably originated in the days of chivalry when knights in mail raised their visors to friends for the purpose of identification. Because of strict adherence to rank, the junior was required to make the first gesture to show they were not challenging the senior. Still, another probability as to the originating of a salute comes from the time when assassinations by dagger were not uncommon. It became the custom to approach each other with raised