Why do they still wear them??
They don't in most courts! Wigs are still worn by Judges really only in the Crown Court (Serious criminal cases).
They are not worn by judges really in any civil cases any more. High Court judges, Court of Appeal Judges and the Supreme Court are all wig free. The Supreme Court judges don't even wear robes in Court - just suits/business dress.
Barristers on the other hand wear wigs also in the Crown Court but in civil cases only during trials, contempt of court hearings and all appeals (Court of Appeal).
Why did they wear them in the first place?
The reasons are slightly different for judges and barristers.
Barristers is because idea was the identity of the advocate is irrelevant - there should be uniformity and focus on the submissions and the words.
For judges, it is more about anonymity. You may laugh about it in the modern world where you can google almost anyone's picture - but if you are an aggressive defendant hanging around outside court, you'd be surprised how different judges look without a wig. It's particularly true for men - as baldness/hair is totally concealed.
For both, there is also the importance of creating a formal and serious atmosphere so defendants facing the law realise it is a serious matter.