We consider the surface area of the entire mass of mercury, including the part of the surface that is in contact with the glass, because mercury does not adhere at all to glass. So the surface tension of the mercury acts over its entire surface area, including where it is in contact with the glass. If instead of glass, the tube were made out of copper, the situation would be very different. Mercury aggressively adheres to copper. So in a copper tube, the level of mercury at the center of the tube will be lower than at the edges (that is, it would be a concave meniscus). In a situation where the liquid adheres to the walls of its container, we consider the part of the fluid's surface area that is in contact with the container to have "negative" surface tension. The fluid then works to maximize the contact surface area. So in this case increasing the area in contact with the container decreases rather than increases the potential energy. That decrease is enough to compensate for the increased potential energy associated with lifting the fluid near the walls of the container.