First of all, there's no reason that an agent has to do the *greedy action*;  Agents can *explore* or they can follow *options*.   This is not what separates on-policy from off-policy learning.

The reason that Q-learning is off-policy is that it updates its Q-values using the Q-value of the next state $s'$ and the *greedy action* $a'$.  In other words, it estimates the *return* (total discounted future reward) for state-action pairs assuming a greedy policy were followed despite the fact that it's not following a greedy policy.

The reason that SARSA is on-policy is that it updates its Q-values using the Q-value of the next state $s'$ and the *current policy's* action $a''$.  It estimates the return for state-action pairs assuming the current policy continues to be followed.

The distinction disappears if the current policy is a greedy policy.  However, such an agent would not be good since it never explores.

Have you looked at the book available for free online?  [Richard S. Sutton and Andrew G. Barto. Reinforcement learning: An introduction. Second edition, 
MIT Press, Cambridge, MA, 2018.][1]


  [1]: https://drive.google.com/file/d/1opPSz5AZ_kVa1uWOdOiveNiBFiEOHjkG/view