There are two effects operating in both compounds: resonance and the inductive effect.
-NH2
In aniline, the nitrogen is more electronegative than carbon, and tends to pull away some electrons from the ring by the inductive effect.
However, nitrogen also has a lone pair of electrons. It can donate this lone pair onto the carbon, and quite a few resonance structures can be drawn. Resonance is much more significant than the inductive effect in aniline. -NH2will give more electron density than it takes, and thus NH2 is an electron donating group.
-NO2
In nitrobenzene, on drawing the structure, we see that the nitrogen in -NO2 already has a positive charge, because it has given its lone pair to one of the oxygens. It can no longer give its lone pair into the ring. Also, due to the positive charge, it will be unstable, and the nitrogen will try to pull electrons away from the ring to reduce the positive charge. It can do this by both resonance (double bond shifts away from C=C to become C=N) and by the inductive effect. Thus, -NO2 is very good at pulling electrons away.