Battery storage units (BSUs) are usually used to perform a single function in most planning studies related to microgrids (MGs). This paper presents an effective methodology to use the BSUs to perform multi-function including supply/demand matching and energy arbitrage. This is done according to a system policy containing all possible scenarios to fully utilize the BSUs to maximize the benefit. In the proposed work, the optimal sizing of the MG system under study containing wind turbines (WTs), photovoltaic system (PV), BSUs, and diesel units (DUs) is obtained. The main objectives of the proposed methodology are; 1) minimizing the total costs of the MG, 2) minimizing the harmful gas emissions, and 3) minimizing the accumulated power difference between the generation from renewable energy systems (RESs) and the demand. Due to the stochastic behavior of the output from the RESs, the uncertainties of wind speed, solar irradiance, and temperature are considered in the study. Two modes of operation of the MG (grid-connected and islanded) and the demand side management (DSM) are also considered. The problem is formulated as a constrained nonlinear optimization problem and is solved using two metaheuristic optimization algorithms, Moth-Flame Optimization (MFO) and Hybrid Firefly and Particle Swarm Optimization (HFPSO). Moreover, the uncertainties in the different parameters are considered by using the Latin Hypercube Sampling (LHS) method to generate samples of wind speed, solar irradiance, and temperature. To examine the proposed methodology, different case studies are presented and discussed. Moreover, the results of the used two algorithms, MFO and HFPSO, are compared to show their effectiveness in solving the proposed problem and assure the optimal solution. The optimization problem is implemented and solved using MATLAB software.