One theory is that it came from Roman soldiers' shading their eyes from the intense light that was supposed to shine from the eyes of their superiors. Another theory is that it came from when men-at-arms wore armor--a friendly approach would include holding the reins of the horse with the left hand while raising the visor of the helmet with the right, so that one could be recognised. A third theory is that the salute, and the handshake, came from a way of showing that the right hand (the fighting hand) was not concealing a weapon.
The most widely accepted theory is that it evolved from the practice of men raising their hats in the presence of officers. Tipping one's hat on meeting a social superior was the normal civilian sign of respect at the time [1]. Repeated hat-raising was impractical if heavy helmets were worn, so the gesture was stylised to a mere hand movement.
The naval salute, with the palm downwards, is said to have originated because the palms of naval ratings, particularly deckhands, were often dirty through working with lines. Because it would be insulting to present a dirty palm to an officer, the palm was turned downwards.
http://en.wikipedia.org/wiki/Salute#Hand_Salutes