In recent years, due to the economic and environmental problems, the integration of distributed energy resources, especially renewable energy resources, has been increased in the power systems. The uncertainty of renewable energy resources and their low capacity, have made them unable to participate in electricity markets. In the past, distributed generation resources were operated independent of the system operator which lead to reduced efficiency of the power system, and impose more cost to customers for energy. Therefor, the concept of Virtual power plant has been created to increase visibility and control of distributed energy resources, so that it can link the distributed energy resources with the electricity markets and the system operator, by establishing an appropriate structure. Virtual power plant make distributed energy resources visible in the power system by aggregating their capacity, using Telecommunication channels and creating technical and economic scheming. Virtual power plant definit different optimization problems in order to plan and control its elements for participation in the energy and ancillary services markets. In this thesis, we first introduce various aspects of the power plant, the necessity of its establishment and expressing its different types. Then, we model the elements of a typical virtual power plant, including distributed generation sources, renewable energy resources, energy storage, and controllable and uncontrollable loads. Then, an optimization problem is proposed to dispatch controllable sources in real time so that changes in renewable energy resources and electrical loads are compensated and virtual power plant can execute its contracts. We also introduce models for the day ahead and balance markets and expand our model to a bi-level optimization problem. In summary, we study the operation of a virtual power plant in order to participate in energy markets while the models of day ahead and balance markets are considered. Keywords: virtual power plant, distributed generation sources, electricity market, virtual power plant operation, day ahead market, balance market, Probabilistic modeling, bidding in electricity market.