Goats on trees are found mostly only in Morocco. The goats climb them because they like to eat the fruit of the argan tree, which is similar to an olive.
Farmers actually follow the herds of goats as they move from tree to tree, not because it is so strange to see goats in trees and the farmers like to point and stare, but because the fruit of the tree has a nut inside, which the goats can't digest, so they spit it up or excrete it which the farmers collect.
The nut contains 1-3 kernels, which can be ground to make argan oil used in cooking and cosmetics. This oil has been collected by the people of the region for hundreds of years, but like many wild and useful things these days, the argan tree is slowly disappearing due to over-harvesting for the tree's wood and overgrazing by goats.