Hanuman the mighty god, the one who was born as essence of Shiva, how could he serve an ordinary Vanara King? Why was he serving Sugriva, when he could have been the king himself? The answer lies in the story of Sun becoming the guru/teacher of Hanuman.
Lord Hanuman wanted Sun to be his teacher. He wanted the Sun god to teach him all that he saw while travelling from east horizon to the west from morning to night. But the sun god refused saying, he is too busy. He would be travelling the whole day and resting the whole night, he would have time to stop and teach Hanuman.
But the mighty lord was adamant so he increased his size and placed his one foot on the eastern mountains and the other on the western and stood facing the blazing sun god’s chariot. He did so for countless days. Finally the sun god, impressed with Hanuman’s persistence, gave in and taught him all that he knew.
When Hanuman asked the Sun God what should be his guru dakshina, the Sun god simply asked him to take care of his sun Sugriva, who gets bullied by his half brother – Vali. Hanuman promised to serve Sugriva and protect him throughout. That’s how Hanuman became a member of Sugriva’s court.