It is not just a legend: the goats really climb the trees, even if the plant does not have to be too high and must not have a perfectly vertical and smooth trunk. These animals, in fact, like almost all the members of the sub-family of the Caprinae, have a particular base. Unlike that of the horse, the goat one is made up of two sturdy "nails", under which there is a soft but strong bearing.
(Why do they say "save goat and cabbage"?)
When the animal lays the paw on the ground, its weight is transferred to the two nails: these widen so as to present a wider area, which therefore gives a firmer grip on the surface where the animal walks or climbs ( there are also goats "climbers"). In addition, the bearing provides an additional grip, which allows the goats to have a safe foot on any surface. The branches of the trees, especially those curved by the wind of the deserts, have a very rough bark, which lends itself well to the "climbing" skills of goats.
(Goats also learn an accent)