We know the maximum profit will occur at the quantity where the gap of total revenue over total cost is largest. But how can we find such gap?
Shift the profit line parallel downward until it only touches the loss function in only one point. That's the point where the maximum gap occurs.
The maximum occurs where Marginal Cost=Marginal Revenue.
You can see this from basic profit maximization:
$\max Profit = \max (Revenue - Cost) $
We solve by taking first derivatives, call them $D$, and setting to zero.
Hence $D Revenue - D Cost =0$. Note that what we mean by Marginal Revenue and Marginal Costs are just first derivatives of Revenue and Cost, respectively.
So clearly Marginal Cost = Marginal Revenue.
Graphically this means the slope of the cost function equals the slope of the revenue function at the maximum profit point. This is because the first derivative gives the slope of a function.
So shift the revenue function parallel downward toward costs until it only touches on one point. They have the same slopes at that point. This is because a) revenues here are linear (a straight line) and have the same slope everywhere and b) a straight line that touches a function in one point only (called a tangent) has the same slope as the function at that point.
So that tangent point is your profit maximization point.
Similarly, if you shift revenue upward to the other tangent point with costs, you will find the maximum loss point.