In 2D animation, the workflow is divided in to Pre-Production, Production and Post-Production. Here Compositing happens at the Production phase. Other then Compositing, various other departments like, Layout, Animation and Effects also falls at the Production phase.
Since compositing is done only after all the final animation, necessary effects, painted background and foreground are done, compositing falls at the end of the Production. As all the animation, effects, background and foreground are done separately referring to the final layout, they need to be brought and compiled together to a single image/clip for the final rendering. And this process of creatively combining them together is done at the compositing stage.
Good compositing helps in achieving effects like Parallax, illusion of depth in the film and helps in maintaining the desire style for the project throughout.