Within elite level, higher fare class gets the priority for the automatic upgrade. If a fare-class "tie", then conventional wisdom says the earlier purchase gets higher on the list.
Once it becomes a battlefield situation, there is anecdotal evidence that the earliest check-in (i.e. 24 hours) within elite level gets the top of the list.