It is a very slow process taking multiple years, usually. But fundamentally changing the way your mind works and processes problems is key to keeping depression at bay. Negative thought patterns are reinforced in an endless cycle with depression.
Of course external factors play a heavy part in depression too.