The reason Robert's example fails, as acer has noted, is that the Optimization[Maximize] command gets confused by the dummy variable. For this reason, we added the 'variables' option (in Maple 11, if I recall correctly) to deal with the problem. Maximize(g, x=0..2, variables=[x]) should work. The operator form that acer showed also works, as there is no ambiguity about the optimization variables in this case.
I'll also add my thoughts about plotting versus optimization. One very simple way to optimize is to choose some points in the region of interest, sample the objective function at those points and then select the maximum or minimum. That's essentially what you're doing when you use the plot command. The problem with this approach is that you can easily miss the optimum if you choose too few sample points, but you do wasteful computation if you choose too many points. As David pointed out earlier, you have to hope that your plot's sample points hits the actual optimal point.
Most routines in the Optimization package try to do better than the basic approach by using derivative information. These routines necessarily assume the functions are smooth and they find only one local optimum point. (An exception is method=branchandbound, which finds a global solution to unconstrained univariate problems). So it is not generally true that if one can plot numerically, one can also optimize numerically, at least not with these particular solvers.
On the other hand, if you don't have smooth objective and constraint functions, you can use the GlobalOptimization package, which has looser requirements. The routines do require continuity of the objective, but the package often "works well" when even that is not met, though there are no guarantees of success.
Now, having said all this, I'll also add that it's fine to plot a univariate function to locate its maximum, if the function is inexpensive to evaluate and you don't need great accuracy. The method you choose depends on your needs.