Buttonwood has given an excellent answer with a nice demonstrational video. However, there is one point I'd like to emphasize. In ideal gases, the molar mass is directly proportional to the density:
$$PV = nRT \ \Rightarrow \ \frac{n}{V} = \frac{P}{RT} \tag1$$
If the molar mass of a gas is $M$ and its mass is $m$, $n = \frac{m}{M}$ and $\frac{n}{V} = \frac{m}{MV}=\frac{m}{V} \cdot \frac{1}{M} = \frac{\rho}{M}$ where $\rho$ is the density of the gas. Thus, applying this in the equation $(1)$ you get:
$$\frac{n}{V} = \frac{\rho}{M} = \frac{P}{RT} \tag2$$
Thus, under known temperature and pressure, $\frac{\rho}{M} = \text{constant}$ for given ideal gas.
However, for solids (and liquids), this is not true. To calculate density for a given solid, you need more than its molar mass as Buttonwood correctly pointed out (e.g., use Archimedes' principle). You need at least information about crystal packing of the solid (if it is crystalline). My point is some solids pack more tightly compared to others even if they have the same crystal packing structure (e.g., face-centered cubic or fcc). The following example shows how to calculate the density if you know the unit cell dimensions and molar mass, or vise versa.
The molar masses of gold $(\ce{Au})$ and platinum $(\ce{Pt})$ are $\pu{196.967 g mol-1}$ and $\pu{195.084 g mol-1}$, respectively. In solid state, both $\ce{Au}$ and $\ce{Pt}$ consist of the same crystal packing structure called face-centered cubic (fcc). Crystal studies have revealed that the lengths of cubic unit cells ($a$) of $\ce{Au}$ and $\ce{Pt}$ are $\pu{407.82 pm}$ and $\pu{392.31 pm}$, respectively. Prove that $\ce{Pt}$ is denser than $\ce{Au}$.
If you inspect the fcc unit cell closely, you realize that each of the eight corner atoms is shared with eight unit cells while each of the six face-centered atoms are shared with only two unit cells. Thus:
$$\text{Total atoms per unit cell} = 8 \times \frac{1}{8} + 6 \times \frac{1}{2} = 4$$
Since the unit cell is cubic, its volume is $a^3$. Based on the value of $a$ and number of atoms per unit cell for each metal, you can calculate the molar volume of each metal ($V_\ce{Au}$ or $V_\ce{Pt}$). Since $a_\ce{Au} = \pu{407.82 pm}$:
$$V_\ce{Au} = a_\ce{Au}^3 \times \frac{1}{\pu{4 atoms}} \times N_A \\
= (\pu{407.82 pm})^3 \times \left(\frac{\pu{1 cm}}{\pu{10^10 pm}}\right)^3 \times \frac{1}{\pu{4 atoms}} \times \pu{6.022 \times 10^{23} atoms\:mol-1} = \pu{10.21 cm3 mol-1}$$
and similarly, since $a_\ce{Pt} = \pu{392.31 pm}$:
$$V_\ce{Pt} = a_\ce{Pt}^3 \times \frac{1}{\pu{4 atoms}} \times N_A \\
= (\pu{392.31 pm})^3 \times \left(\frac{\pu{1 cm}}{\pu{10^10 pm}}\right)^3 \times \frac{1}{\pu{4 atoms}} \times \pu{6.022 \times 10^{23} atoms\:mol-1} = \pu{9.09 cm3 mol-1}$$
Now, since we know the molar volume of each metal, we can calculate the density ($\rho$) by using $\rho = \frac{M_\ce{M}}{V_\ce{M}}$:
$$\rho_\ce{Au} = \frac{\pu{196.97 g mol-1}}{\pu{10.21 cm3 mol-1}} = \pu{19.29 g cm-3}$$
$$\rho_\ce{Pt} = \frac{\pu{195.08 g mol-1}}{\pu{9.09 cm3 mol-1}} = \pu{21.46 g cm-3}$$
Therefore, platinum is denser than gold $(\rho_\ce{Pt} \gt \rho_\ce{Au})$ even though their molar masses are reversed $(M_\ce{Pt} \lt M_\ce{Au})$.
Note: If you know the densities without knowing molar masses, you can calculate molar masses using the same strategy (only one unknown).