Why are the Rangers protecting the ShireBecause the Ring is/was in the Shire (at that time).
How did they know the Shire needed protecting?
Gandalf alerted them, as you indicated.
If what you say is true, then the Rangers protecting the Shire is a relatively recent thing. But from the Tom Bombadil quote I posted a couple nights ago it seems that the Rangers have been about that business for quite a while.