After Effects by default works in 2D, but you can change a layer so that it can be moved in 3D space. The layer itself is still flat, but (like a postcard moving through space) it can be positioned using x,y and z coordinates where z is depth.
This tutorial explains it well:
You can also do full 3D work in After Effects, but I think this should cover your question.
Alternatively if you want to only work in 2D, sometimes I duplicate a layer, and have one layer at the bottom of my stack, and the duplicate layer at the top, so that when I need to change its apparent "depth", I make one layer invisible and make the other layer visible (by keyframing the Opacity property).