The best way ...
Well, as I think we have agreed: the very best way is to not change core Moodle code if there is any other option. But, sometimes you have to.
So, let us say: the second best way is to use a good code version control system, which in the Moodle world means git https://git-scm.com/. (But really, that is just a more sophistcated way to do what you said: "keeping some patch files around to apply after upgrade". The extra sophistication is really worth having, however.
https://docs.moodle.org/310/en/Git_for_Administrators
https://docs.moodle.org/dev/Git_for_developers
Well, as I think we have agreed: the very best way is to not change core Moodle code if there is any other option. But, sometimes you have to.
So, let us say: the second best way is to use a good code version control system, which in the Moodle world means git https://git-scm.com/. (But really, that is just a more sophistcated way to do what you said: "keeping some patch files around to apply after upgrade". The extra sophistication is really worth having, however.
https://docs.moodle.org/310/en/Git_for_Administrators
https://docs.moodle.org/dev/Git_for_developers