In theory: by elite level, within a level by fare paid, within a fare (if another tie-breaker is needed) by check-in time.
In practice: by elite level, then somewhat random based in part on who's easy to find and perhaps who smiles nicely or at least isn't obnoxious.
I've gotten op-ups when flying alone several times, most recently two weeks ago NRT-SJC (when it was most welcome; thanks, AA). Only once did I get one when flying with a (non-elite) companion. I was given the op-up and said "I'm with X." The agent sighed and upgraded X. On another occasion Y was not upgraded, so I turned mine down.