I think it started in the 17th century when it was fashionable to wear wigs. It was accepted for anyone of that status to wear a wig. Tradition has been kept though. It is maintained for criminal cases in the Crown Court (unless there are children involved in which case the Judge will order wigs and gowns to be removed) but not the magistrates court. It is also worn in the County Court for some civil trials and the High Court upwards. Some cases are not "robed" for instance where the judge sits in chambers or for bail applications and some interim hearings.
The gown of a junior barrister has a token "hood" at the back. This was where the client would slip the barrister's gratuity or fee - a barrister cannot sue for his or her fees.
I have no idea for certain WHY wigs and gowns are maintained. I was told that it was to preserve the anonymity of the lawyers and show that there is no prejudice, all things being equal, at justice. However, I really do not know for certain.