As I discuss in this answer, when Hanuman was a child he saw the sun and thought it was a fruit, so he flew towards it in an attempt to eat it. When Indra saw Hanuman coming, he threw his Vajra (thunderbolt) to strike him down, in the process injuring Hanuman's left cheekbone. Vayu the wind god, who was Hanuman's father, was furious at his son being struck down, so he refused to circulate air in the world anymore. So to pacify Vayu, Brahma revived Hanuman, and then the gods bestowed various boons on him, which are described in the Uttara Kanda of the Ramayana. Here is the boon bestowed by Brahma:
This child will be long-lived and great. He will be invulnerable to my rod of chastisement and to curses uttered by brahmanas.... This best of monkeys will be able to assume any form he chooses. He will be able to go wherever he wants as fast as he wants. His movement will be unimpeded and he will be glorious. In combat he will accomplish hair-raising deeds for Ravana’s consternation and for the pleasure of Lord Rama.
It is this power "to assume any form he chooses" that Hanuman uses at Lanka; this is what he thinks to himself in the Sundara Kanda of the Ramayana:
If I stay here with my usual form, I can be destroyed. The task of the Lord will also be harmed. For that reason, I will become small in my appearance and will enter Lanka at night for the success of the task of Sri Rama.
By the way, other Vanaras, specifically the ones fathered by the gods to help Rama, did have some shape changing abilities, as described in the Bala Kanda of the Ramayana: "They are endowed with guise changing faculties, with bodily might and by their pride and might are identical to lions and tigers, and they are born-free in their movement in forests." But their abilities would presumably have been much more limited, not having received the powerful boon from Brahma. So Hanuman was probably the only one who could assume forms of any size he wished.