From the Official US Army Handbook...
Saluting
The origin of the hand salute is uncertain. Some historians believe it began in late Roman times when assassinations were common. A citizen who wanted to see a public official had to approach with his right hand raised to show that he did not hold a weapon. Knights in armor raised visors with the right hand when meeting a comrade. This practice gradually became a way of showing respect and, in early American history, sometimes involved removing the hat. By 1820, the motion was modified to touching the hat, and since then it has become the hand salute used today.
During your time in the Army, you salute to show respect toward an officer, flag, or our country. The proper way to salute with or without a weapon is described in FM 22-5. The rules of saluting are as follows:
*When you meet someone outside, salute as soon as you recognize that he or she is an officer (or, if you are walking toward the officer, wait until you are about six steps away).
*Salute all officers (recognized by rank) in official vehicles identified by special plates or flags.
*Salute only on command when in formation.
*If in a group, and an officer approaches, the first soldier to recognize the officer calls the group to attention and all personnel salute.
*If you approach an officer while you are double-timing alone, assume quick time march and render the hand salute. When the salute is returned, execute order arms and resume double-timing.
*The salute is always initiated by the subordinate and is terminated only after acknowledgment by the individual being saluted.
*Accompany the salute with an appropriate greeting, such as, "Good morning/afternoon, sir/ma'am."
*Salutes are not required to be rendered by or to personnel who are driving or riding in privately owned vehicles.
*It is not customary for enlisted personnel to exchange salutes, except in some ceremonial situations.
*Never render a salute with a noticeable object in your mouth or right hand.
*If you are on detail, and an officer approaches, salute if you are in charge of the detail. Otherwise, continue to work. If you are spoken to, then come to the position of attention.