How does one derive $\Delta_\mathrm r G^\circ = -RT\ln(K)$? I don't see the connection mathematically or conceptually; how does Gibbs at all relate to the equilibrium constant of a reaction?

This was part one of a three part question which I've split into 3 individual questions as advised. I left this question as the one associated with the original title; the other one is here and I will edit the third in when my post cooldown allows me to post it, if anyone wishes to visit those as well now.