Not a historian, but do have some military background.
During the average day, if you are wearing headgear such as a beret or peaked cap, it is a mark of respect to salute your superiors and for them to acknowledge it by returning it. When indoors, you should not be wearing headgear - it's considered a massive faux pas (with some exceptions, such as OOD) The RN takes this seriously - a rating going to Captains Table for being a naughty matelot has his cap on, but is ordered to remove it prior to proceedings getting underway.
Without headgear, you are not supposed to salute, at all. Standing up and to attention is a reasonable way of showing the due respect for the superior rank, even if you detest the person.
As Dick Winters said, "We salute the rank, not the man."
OOD = Officer Of the Day
Desmond Morris argues that sitting is a sign of dominance:
"The power of the seated position is an unsual element in our behaviour. No one may sit if the 'king' is standing. When the 'king' rises, everyone rises. "
Similar to a judge entering a room. "All rise!"
http://www.evolbiol.ru/large_files/naked_ape.pdf
p. 148 for this particular assertion but there's more reading that leads up to this. Wish I could elaborate but it's been 20 years since I read it. The idea really stuck with me though since I've seen so many real life examples.
It should also be noted that standing for respect is not something restricted to the military. It is done in courtrooms, in schools when a teacher enters the classroom (at least in Turkey and Japan), during a national anthem. So, even though I do not know the military context, I believe it just a continuation of an old tradition done for showing respect.