(O you who believe) This is a commencement in explaining the laws pertaining to what occurs between them and others, following what related to their own selves.
(Be upholders for Allah) i.e., fulfill His necessary rights abundantly. It is said: Let it be your habit to uphold the truth, within yourselves through righteous deeds, and regarding others through enjoining good and forbidding evil, seeking the pleasure of Allah, the Exalted.
(Witnesses for justice) i.e., with fairness. It is also said: Callers unto Allah, the Exalted, manifesting His religion with true proofs.
(And let not) do not let (the hatred of a people) i.e., the intensity of your hatred for them (incite you) into (not being just). So, do not testify against their rights with justice, or transgress against them by committing what is not lawful.
(Be just) O you who believe, both toward your allies and your enemies. Some have limited this to enemies, based on the report that when Makkah was conquered, Allah, the Exalted, charged the Muslims with this verse so that they would not requite the disbelievers of Makkah for what had passed from them, and that they should be just in word and deed.
(That) referring back to the justice implied by the verb. It is either justice in the absolute sense—thus including the justice indicated by the occasion of revelation—or justice specifically with the disbelievers.
(Is nearer to righteousness) i.e., more conducive to it, for righteousness is the ultimate state of obedience, and this is the most fitting of obediences to it; thus, the proximity between them is the suitability of obedience to obedience. It is also possible that its proximity to righteousness is considered in the sense that it is a means of grace for it, as the cause leads to the effect; it is like the final part of the cause. The lam (in lil-taqwa) is like its counterpart in your saying: "He is close to Zayd," for the sake of specification; it is a complementary lam that could be represented by min or ila.
The scholar Al-Raghib exerted himself in addressing the verse and said: If it is said, "How can He, Glorified be He, say 'nearer to righteousness,' when the superlative form (af'al) is only used for two things that share in one matter, with one having an advantage over the other, while we know that nothing from righteousness or doing good is separate from justice?" It is replied: The superlative form, even if it is as you mentioned, may be used upon the premise of the addressee's belief regarding a thing in itself, in order to silence his argument and reveal his confusion. So it is said to one who believes, for example, that Zayd has merit—even if he has none, but he cannot deny that Amr is more meritorious than him—"Serve Amr, for he is more meritorious than Zayd." Upon this came the saying of Allah, the Exalted: "Is Allah better, or what they associate?" knowing that there is no goodness in what they associate. The clause serves as the justification for the command to be just, and He declared it to them explicitly to emphasize and reinforce it.
He, Glorified be He, commanded righteousness with His saying, Exalted be He: (And fear Allah), following His explanation that justice is nearer to it, showing concern for its status and alerting that it is the foundation of the entire matter.
(Indeed, Allah is Acquainted with what you do) of deeds, and He will reward you for them. A similar verse has preceded in [Surat] An-Nisa; He did not settle for that [previous mention] due to the increased importance of justice and the exaggeration in extinguishing the flames of anger. It is also said: [It is repeated] because of the difference in the occasion; the first was revealed concerning the polytheists, and this one concerning the Jews. Some investigators mentioned a reason for presenting justice there and delaying it here, which is that the verse in An-Nisa was brought in the context of bearing witness against one's own self, parents, and relatives; thus, it began with justice, which is fairness without favoritism to one's self, parent, or kin. The one here was brought in the context of abandoning enmity; thus, it began with upholding [the cause] for Allah, the Exalted, because it is more of a deterrent to the believers, then followed it with witnessing for justice, bringing in each context what is appropriate for it.