Horses do not attack people for no reason. Very occasionally, they may nip, kick or worse -- but not for no reason. (Incidentally, if a horse really did decide to attack properly, you would be lucky to survive at all -- they are very large and very powerful indeed). Generally they are not really attacking, but warning you.
If this is your own horse, the advice from Stumped is good.
If this is someone else's horse, there could be many different reasons.
For example, the horse may have been ill-treated, and is taking this out on any human it can find. Luckily this is very rare indeed.
More common is a horse who is not aggressive, but is "pushy". It has been fed too many polos or other treats, and has learnt to fuss visitors until they produce the goods. If they don't, the horse is disappointed and may nip. This is a bigger problem with small and unconfident people (such as children) -- the horse immediately sees who is boss. Stamping your feet slightly as you walk seems to help here. It is this which is the reason for the bylaws here in the New Forest preventing the public feeding the animals.