You need to do an experiment to determine how much ##Mg## and ##O## are in a sample of the compound.
For example you might heat a known mass of magnesium in a crucible and determine the mass of oxide formed.
EXAMPLE
Assume that you heated 0.297 g of magnesium and obtained 0.493 g of the oxide. What is the empirical formula of magnesium oxide?
Solution
The empirical formula is the simplest whole-number ratio of atoms in a compound.
The ratio of atoms is the same as the ratio of moles. So our job is to calculate the molar ratio of ##Mg## to ##O##.
##Mass of Mg = 0.297 g##
##Mass of magnesium oxide = mass of Mg + mass of O##
##0.493 g = 0.297 g + mass of O##
##Mass of O = (0.493 0.297) g = 0.196 g##
##Moles of Mg = 0.297 color(red)(cancel(color(black)(g Mg))) 1 mol Mg/(24.3color(red)(cancel(color(black)( g Mg)))) = 0.012 22 mol Mg##
##Moles of O = 0.196 color(red)(cancel(color(black)(g O))) 1 mol O/(16.00 color(red)(cancel(color(black)(g O)))) = 0.012 25 mol O##
To get this into an integer ratio we divide both numerator and denominator by the smaller value.
From this point on I like to summarize the calculations in a table.
##Elementcolor(white)(Mg) Mass/gcolor(white)(X) Molescolor(white)(Xll) Ratiocolor(white)(mll)Integers##
##stackrel(-)(color(white)(m)Mg color(white)(XXXm)0.297 color(white)(X)0.012 22
color(white)(X)1color(white)(Xmmmm)1##
##color(white)(m)O color(white)(XXXXll)0.196 color(white)(m)0.012 25 color(white)(X)1.002 color(white)(XXX)1##
There is 1 mol of ##Mg## for 1 mol of ##O##.
The empirical formula of magnesium oxide is ##MgO##.
Here is a video that illustrates how to determine an empirical formula.