How come, in the heat equation, the maximum can not be attained on the upper boundary of the rectangle we construct?

It's actually a bit tricky to prove that the maximum can only occur on three sides of the rectangle. For a full proof of this, please see page 312 of Olver's Introduction to Partial Differential Equations. The proof he gives is somewhat detailed and technical, so I'm purposefully avoiding these details in my answer. Either read the linked text, or ask me, to see the full calculations.

Theorem 8.6. Let $\gamma>0$. Suppose $u$ is a solution of the forced heat equation $$\partial_tu=\gamma \partial_x^2u+F$$ on the open rectangular domain $$R=\{(t,x)\in \Bbb{R}^2 \mid a< x< b~,~0< t< T \}$$ Assume that no new heat is introduced, i.e, the forcing term is nowhere positive, satisfying $F(t,x)\leq 0~~\forall(t,x)\in R$. Then, the maximum of $u$ on the closed rectangle $\overline{R}$ can only be attained when $t=0,~ x=a,$ or $x=b$. (Note that in the case of $F=0$ we are reduced to the standard heat equation.)

Proof: Let us first work under the stronger assumption $F <0$. This gives us the strict inequality $$\partial_tu<\gamma\partial_x^2u\tag{1}$$ This is enough to show that there cannot be a maximum in the open domain $R$. You do this by supposing that $u$ attains a maximum at the point $(t_\star,x_\star)$ and show that the requirements for this point to be a maximum are incompatible with the above inequality. Again see page 312 of the linked text for the full details.

It is still necessary to exclude the possibility can occur when $t=T$. Take a point on the right edge, $(T,x_\star)$ where $a<x_\star<b$. Note that because we are on a boundary point for our $t$ variable, the conditions for $(T,x_\star)$ being a critical point can be slightly weakened. Instead of $\partial_tu(T,x_\star)=\partial_xu(T,x_\star)=0$ we can weaken the requirement in $t$ to $$\partial_t u(T,x_\star)\geq0 \tag{2a}$$ Now in order for it to be a maximum, and not a minimum, we need the graph of $u$ to be curved downwards, i.e $$\partial_x^2u(T,x_\star)\leq 0\tag{2b}$$ However, the requirements $(2\text{a}),(2\text{b})$ are incompatible with our starting inequality $(1)$, and so we conclude no maximum can occur on the right edge.

So, we've gotten the desired result under the assumption $F<0$. How do we generalize this to $F\leq 0$? We do it with a non-obvious trick. Let $u$ be a solution to the same forced heat equation, but with the weaker requirement $F\leq 0$, on the same open domain and let $$v(t,x)=u(t,x)+\varepsilon x^2$$ $\varepsilon$ being a small positive constant (instead of $x^2$, we can use any non-negative convex function we want, but $x^2$ keeps things simple). You can check that, letting $\tilde{F}=F-2\gamma\varepsilon$ that $$\partial_t v=\partial_x^2v+\tilde{F}$$ And since $\tilde{F}<0$ strictly, our previous result follows.

Loosely speaking, letting $\varepsilon\to 0$ this shows the desired result. I can expand on the details if you wish.