"Sir" is meant to be a term of respect, from a subordinate to a higher ranking person, implying the latter is in authority. These days it is common for a uniformed person, often security, to say "Sir" then proceed to order them to do something. To question this results in a more insistent "Sir!" and a reiteration of the instruction, with the implication of obligatory obedience or deleterious ramifications. In other words, "obey me or else ... " Obviously, sir is not respected or in a position of authority. The uniformed officer is using his/her absolute authority to browbeat sir, without allowing him the opportunity or right to negotiate the rationale of the instruction, to defend himself against blind obedience. In other words, 'sir' is derogatory when used by officious thugs who pretend to the greater public good. People are not idiots. To treat people with respect, is to have regard for their autonomy, listen to them, not order them about.
My entire childhood I was told that if I called some one Ma'am or sir I was in fact calling that person old and was disrespectful so I made sure as to learn there names or address them by the title that was given. So as such grandparents were addressed as Grandma and Grandpa and aunt and uncles were addressed as Aunt and there first name.
Where I grew up calling someone ma'am or sir was a sign of disrespect. It showed you didn't take the time to learn their names or could care less, or even that you were under minding them by saying it sarcastically. Calling a men or women under the age of 50 showed you thought they were old and people would get extremely offended. I moved to the south of the US and it was complete opposite. Here its a sign of respect, but I still feel like I'm mistreating someone if I say it.
They are a sign of respect and a reflection of authority. If someone outranks you professionally, is elderly, or in a position of authority, then referring to them as sir or ma'am should be automatic. It is common courtesy and reflects manners by those who use these terms. Unfortunately, the younger generation is not being raised with the same values as past generations and that reflects in the lack of manners and respect displayed by many youth.
In general the terms sir and ma'am are used as a sign of respect when speaking to someone older than you. They are also used to show respect for your superiors in the military, or to speak to a customer in a retail setting. As a general rule of thumb, they should be used anytime you're addressing someone you don't know well, or whose name you do not know or can't remember.
"Sir" and "ma'am" serve a good role in allowing one to address a person he does not know by name or at all. To say, "Excuse me, sir" is much more pleasant to hear than "Hey you." A person can use them in a disparaging way, but that does not make them completely derogatory.
They are a sign of respect and formality. This is why the military call those ranked higher than them 'sir' or 'ma'am.' It's not being derogatory but showing that you have respect for their seniority. If more of today's youth would start calling their elders by 'sir' or 'ma'am,' I believe the older generation would begin having more respect for the younger generation. It goes both ways.