Ordering mechanism and quantum anomalous Hall effect of magnetically doped topological insulators