The OP assumes that people only address their direct superiors and bosses, or at least people of superior rank, as "sir".
This question at Science Fiction and Fantasy Stack Exchange assumes the same thing:
https://scifi.stackexchange.com/questions/87040/star-trek-tos-officer-status/87534#87534
The OP finds an example of Spock calling Dr. McCoy sir in one episode perplexing as Dr. McCoy is not his direct boss nor of higher rank. Dr. McCoy is the ship's surgeon and head of the life sciences department.
Both are called to testify in Kirk's court martial in the first season episode "Court Martial":
SHAW: I now call Doctor McCoy to the stand.
COMPUTER: Service rank, Lieutenant Commander. Position, Ship's Surgeon. Current assignment, USS Enterprise. Commendations, Legion of Honour. Awards of valour. Decorated by Starfleet surgeons.
And:
SHAW: I call Mister Spock.
(Spock hands over his data chip, sits down and puts his hand on the lie detector.)
COMPUTER: Spock, serial number S179-276SP. Service rank, Lieutenant Commander. Position, First officer, science officer. Current assignment, USS Enterprise. Commendations, Vulcanian Scientific Legion of Honour. Awards of valour. Twice decorated by Starfleet command.
Both Doctor McCoy and Mr. Spock have the rank of lieutenant commander. Spock is not a member of the medical staff or the life sciences department aboard the Enterprise. Instead, as the First Officer or executive officer of the ship he might possibly be Dr. McCoy's boss, and as the science officer he might possibly be the Boss of Dr. McCoy as head of the life sciences department.
In the second season episode "The Immunity Syndrome" Spock makes what he expects to be his last message ever:
SPOCK: Personal log, Commander Spock, USS Enterprise. I have noted the passage of the Enterprise on its way to whatever awaits it. If this record should survive me, I wish it known that I bequeath my highest commendation and testimonial to the captain, officers, and crew of the Enterprise. The finest starship in the fleet.
Kirk on the Enterprise also makes a recorded statement which he thinks might be his last:
KIRK: (also making a log entry) We have arrived at the chromosome body in the nucleus of the organism. If we should fail in our attempt to destroy it, or be unable to free ourselves, I wish to record my recommendations for the following personnel, that they receive special citation. Lieutenant Commander Leonard McCoy, Lieutenant Commander Montgomery Scott, officers Chekov, Kyle, Uhura, and my highest commendation for Commander Spock, Science Officer, who gave his life in the performance of his duty.
Note that Kirk names two officers as lieutenant commanders in rank but names Spock as commander in rank. The always precise Spock also gives his rank as commander. It thus seems very likely that Spock has been promoted from lieutenant commander to commander between those two episodes.
In the second season episode "The Trouble With Tribbles":
MCCOY: I like them better than I like you.
SPOCK: Doctor?
MCCOY: Yes?
SPOCK: They do have one redeeming characteristic.
MCCOY: What's that?
SPOCK: They do not talk too much. If you'll excuse me, sir.
Spock is clearly a little annoyed at Dr. McCoy, and is also McCoy's equal or superior in this setting, but he politely asks to be excused and calls McCoy "sir".
In the first season episode "What Are Little Girls Made of?" Dr. Korby captures Captain Kirk and makes an android double of Kirk, planning to use the double to take command of the Enterprise. Kirk and the android double of Kirk talk:
KIRK: Well, there's one difference between us. I'm hungry.
KIRK2: The difference is your weakness, Captain, not mine.
KORBY: One at a time, gentlemen. Captain?
KIRK: Eating is a pleasure, sir. Unfortunately, one you will never know.
KIRK2: Perhaps, but I will never starve, sir.
Kirk and his android double are on opposite sides of a conflict, and so neither is subordinate to the other, but bothp politely call the other "sir".
And here is an example of a phrase where someone calls someone else "sir " while insulting them:
https://www.urbandictionary.com/define.php?term=cad%20and%20bounder1
And here is a link to an article containing other examples of calling someone "sir" while insulting them:
https://www.salon.com/1996/02/10/sanders_2/2
Therefore, in 19th and 20th century English it was proper to call someone "sir" who wasn't your direct superior and wasn't of higher rank than oneself, and so to call someone sir when angry with them, when on opposite sides in a conflict, and even when insulting them.
Note that in Christophe Stobbe's answer a quotation from The Winter's Tale has Leontes address his young son as "sir page" in one line and "sweet villain" in the next line, which may seem like rather contradictory expressions to modern readers.
And thus I find it easy to believe that in the English of Shakespeare's era about 1600 it might have been the rule to always address someone entitled to be called "sir", such as a knight, as "sir" as a matter of course, even by their worst enemies or by someone much higher ranking than them.