Allah The Glorious knows everything which is necessary for human beings, that is why He orders us to do it as a compulsory act and not an optional act e,g. Praying the daily prayers, fasting during the month of Ramadan, paying Khums and Zakat, etc.
Wearing Hijab is compulsory on every Muslim female because it is essential to protect her dignity and save her and the society from harm. Allah says in Quran: O Prophet! Tell your wives and your daughters and women of the believers to draw their cloaks ( veils) all over their bodies, so that they will known ( as pious women) so that they will not be harmed. (Sura 33, Verse 59).
We as Muslims believe in the Absolute Wisdom of Allah (SWT) which means that every order from Him is based on full wisdom for our benefit.