You should know the molar mass of each chemical element in the compound. M(el) Then you should count how many times each element appears in the compound. N Multiply M(el) by N respectively M1*N1, M2N2, M3N3 for each element and then add these numbers.
For example: KMnO4 M(K) = 39 g/mol, M(Mn) = 55 g/mol, M(O) = 16 g/mol. There are four atoms of the Oxigen in this compound, thus M(KMnO4) = 1* 39 + 1 * 55 + 4 * 16 = 158 g/mol.