'In the days of fair weather we led Gollum through the woods; and there was a high tree standing alone far from the others which he liked to climb. Often we let him mount up to the highest branches, until he felt the free wind; but we set a guard at the tree's foot. One day he refused to come down, and the guards had no mind to climb after him: he had learned the trick of clinging to boughs with his feet as well as with his hands; so they sat by the tree far into the night.
'It was that very night of summer, yet moonless and starless, that Orcs came on us at unawares. We drove them off after some time; they were many and fierce, but they came from over the mountains, and were unused to the woods. When the battle was over, we found that Gollum was gone, and his guards were slain or taken. It then seemed plain to us that the attack had been made for his rescue, and that he knew of it beforehand. How that was contrived we cannot guess; but Gollum is cunning, and the spies of the Enemy are many. The dark things that were driven out in the year of the Dragon's fall have returned in greater numbers, and Mirkwood is again an evil place, save where our realm is maintained.
'We have failed to recapture Gollum. We came on his trail among those of many Orcs, and it plunged deep into the Forest, going south. But ere long it escaped our skill, and we dared not continue the hunt; for we were drawing nigh to Dol Guldur, and that is still a very evil place; we do not go that way.'
The Fellowship of the Ring, LoTR Book 2, Ch 2, The Council of Elrond
How did Gollum know that a rescue party would come and save him?