The real-time pricing policy can incentivize the electricity users to dynamically change or shift their electricity consumption, thereby improving reliability of the grid. In the smart grid infrastructure, aggregators between the electricity suppliers and users control the users' electricity consumption by dynamically setting electricity price. This work aims at maximizing the overall profit of an aggregator in a billing period by designing a real-time pricing policy. The aggregator pre-announces a pricing policy for an entire billing period, then in each time interval of the billing period, the electricity users (i.e., both residential and EV users) try to maximize their own utility functions based on the pricing model in the current time interval and the awareness of the other users' behaviors. We first formulate a nested two-stage game between the aggregator and the users for each time interval in a billing period, in which the sub game perfect equilibrium can be found. Then, based on backward induction, a dynamic programming algorithm is presented to derive the optimal real-time pricing policy for maximizing the aggregator's overall profit. Different from other works, a battery energy storage system (BESS) is integrated with the aggregator to buffer the mismatch between supply and demand and to improve reliability of the grid. More importantly, this work derives the optimal pricing policy for an aggregator from a global point of view, taking into account the BESS energy state variation in a billing period. Simulation results show that the optimal pricing policy can achieve up to 24.3% improvement on the aggregator's overall profit.