A microgrid, as a controllable intelligent electric network, composed of distributed energy systems (DERs), energy storage systems (ESS) and controllable loads, require an Energy Management System (EMS) as a central entity responsible for coordinate control of DERs, for the dispatch of units under supply and demand uncertainty, for managing instantaneous active power balance, power flows and network voltage profiles, among others. Considering this central control structure, in this master dissertation it is proposed an Energy Management System composed of two optimization stages: a long-term and a short-term stage.The main function of the long-term stage is to solve the energy management problem considering an operational horizon of 24-hours to minimize simultaneously the operational cost and the power losses. To do this, it is used the Non-dominated Sorting Genetic Algorithm II (NSGA-II) complemented with a Quadratic Programing (QP) algorithm, to reduce the final complexity of the energy management problem. For the short-term stage, it is used a QP algorithm. The main function of the short-term stage is to guarantee power balance and reduce the impact of the forecast error in the operation of the distribution system.To develop the optimization algorithm MATLAB and GridLab-D are used to implement and simulate the EMS in a microgrid composed of a residential distribution network including batteries, renewable and fuel-based generation systems. To evaluate the developed EMS two main cases are studied, a perfect forecast case and real operational case. Finally, dynamic simulations are carried on in GridLabD to technically assess the impact of the optimal solution in the distribution system.