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.