While not a “rule,” yelling “Fore” is good etiquette and a safety precaution. Getting hit by a golf ball can be very painful, and on occasion cause bodily harm or even death.
While a good, loud “Fore” is sufficient, an even better idea is to also yell “left” or “right” and gesture in the direction of the ball flight. You will occasionally see tour pros do that when their ball goes awry.
Like much of the terminology of golf, the origin of Fore is a mystery.
It may be that “Fore” is simply a derivative of “before,” or “forward”, either of which makes perfect sense. Others suggest that it has nautical origins, with aft being the rear of a ship and fore indicating the front.
Another theory bandied about is that Fore comes from a 19th century military warning, “beware before,” which was supposed to alert friendly infantry to hit the dirt to avoid artillery shells firing over their heads.
The one that makes the most sense to me, however, is that it is related to the use of forecaddies. Forecaddies are employed to wait down the fairway to spot where a golfer’s ball lands. Yelling “Fore” may have been a way to alert the Forecaddies that the ball was on its way.