Spoilers from here. The ending is described in full.
The box contains the severed head of Tracy Mills, David Mills's wife. The film never shows it. Somerset opens the box, reacts, and everything after that is conveyed through his attempt to stop what he knows is about to happen.
How the last two sins work
John Doe has been constructing a sequence of murders, each punishing one of the seven deadly sins. Five are complete when he surrenders: gluttony, greed, sloth, lust and pride.
He then offers to lead the detectives to two further bodies, and drives them into open desert. The final two sins are not additional victims. They are Doe and Mills.
Envy is Doe's own. He tells Mills that he envied his ordinary life and his wife, and that he tried to live it and could not. Killing Tracy is the act of envy, and it makes Doe the sixth victim by his own design.
Wrath is Mills. Doe needs him to shoot, which completes the seventh sin and finishes the work.
Why Tracy is the target
Doe reveals that she was pregnant, which Mills did not know and Somerset did. She had told Somerset earlier and asked him not to say anything while she decided.
That detail is what closes the trap. Mills might have survived the first revelation. The second removes any possibility of him not firing, and Somerset is left arguing against a decision he can see coming and has no way to prevent.
Why the film does not show the head
Because it does not have to, and because showing it would replace an idea with an image. Everything you need is in Somerset's face, in his refusal to say what he has seen, and in his effort to get Mills away from the box.
The restraint also protects Tracy from becoming a piece of spectacle. The film's only interest in the contents is what they do to the man who is about to be told.
Does John Doe win?
Yes, and the film is unusually direct about it. He completes his work, dies as he planned, and turns a police officer into the instrument of his final sermon.
The only thing he does not control is whether anyone takes the message seriously, and Somerset's closing narration answers that. He quotes a line about the world being a fine place and worth fighting for, and says he agrees with the second part. That is as close to a rebuttal as the film offers, and it is deliberately thin.
The endings that were considered
Alternative versions were discussed during production, including ones in which the box's contents differed or Mills did not shoot. The version that survives is the one the script was built for, and the studio's reported reluctance about it is a reasonable indication of how unusual it was to release.
Why the ending still works
Because it withholds the two things audiences expect: the image, and any sense that the detectives achieved something. Somerset is retiring and cancels it. Mills has lost everything. The rain has stopped, which is the only visual change in the entire film, and it stops for the murderer's convenience rather than anyone else's.
Why the desert
Every earlier scene is enclosed, dark and raining. The final act takes place in flat open ground under a clear sky with power lines running to the horizon, and nothing to look at in any direction.
There is nowhere to hide and no possibility of anyone intervening, which is exactly what Doe requires. He has chosen a location where his plan cannot be interrupted, and the visual break from the rest of the film tells you the rules have changed before anyone speaks.
Somerset’s decision
He was retiring in seven days and had already handed in the paperwork. The final scene has him saying he will be around, which is the film's only concession.
It is a small one. He remains because Mills is gone and somebody has to, not because anything he has seen suggests the work achieves much. The film gives him no victory and lets him choose to continue anyway.