(The picture is from Exercise 5.1.16 in QingLiu's Algebraic Geometry and Arithmetic Curve) All the proofs I saw on any reference usually start with "the problem is local on $T$ and on $S$ so we may suppose $S$ and $T$ are affine". I used to accept it handwavely but when I want to write a rigourous proof on my own I can't make it rigourous.
Assume we have proved the theorem for the case $T$ and $S$ are affine, can anyone provide a rigorous proof for this theorem?

OK I got it. It involves a lot of checkings of commutativity. We are going to need the following 2 claims as basic tools for commutativity.
(The counter didn't start with 1 because I just copied the picture from my notes) They can be proved using the push-forward-pull-back adjointness.
We first reduce to the case $T$ is affine via the following claim:

Then we reduce to the case $S$ is affine.
At the demand of the comment by @BernyPiffaro, I add the following proof about the case $S$ and $T$ are affine.
Assume now $S$ and $T$ are affine.