Track the minimum price seen so far as you scan left to right.
At each day, your potential profit is today's price minus the minimum price you've seen. You're essentially asking: "If I bought at the lowest point before today, what profit would I make selling today?"
Update the maximum profit whenever you find a better opportunity. The minimum price can only decrease or stay the same as you move forward.
With prices = [7, 1, 5, 3, 6, 4]: after day 2, minimum is 1. On day 5, profit would be 6 - 1 = 5. That's the best you can do.