Answer is http://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane.27s_algorithm
The idea behind this algorithm is that we assume we know the true maximal subarray of the problem for an array of length N, AND we know the maximal subarray which starts at either end. (rationale: If we assume we know the maximal sequences which touch the ends, and we gradually tack on more elements to the end, then we cannot miss the true maximal subarray since at some point the element added to the end will be the same element as the endpoint of the true maximal subarray.) Tacking on an extra element might increase the length of the longest subarray attached to the end. If the subarray's sum goes below 0, we reset it. If it goes above our current best candidate solution for the true maximal subarray, we replace our best candidate solution.
Alternatively, we can use the power of integration. (It's an O(N) pass you can do for free; you can also differentiate for free.) Then we look at all the extrema, searching for the two extrema MIN and MAX, such that MAX is to the right of MIN (or else you'd be finding the most negative sum), and also such that MAX-MIN (the consecutive sum) is greatest. You could brute-force this subproblem (and still be O(N) if the number of extreme number less than sqrt(N)), or you can solve it in a more efficient manner [could use some help here].