This compensation is called "Miller compensation". Purpose of the resistor is to eliminate the RHP zero that occurs without the resistor. The RHP zero is a very bad thing for stability hence needs to be eliminated. There are techniques for eliminating the RHP zero and addition of the resistor is one option. The resistor value is usually set such that the RHP zero moves to the real axis.
Take a look at for example this article to understand the RHP zero and why it is bad for stability
https://pallen.ece.gatech.edu/Academic/ECE_6412/Spring_2004/L130-OpAmpCompII(2UP).pdf