The miller is very proud of his daughter, so when the king passes through, the miller lies to the king and says that his daughter could spin straw into gold.
The king takes the millers daughter to his castle to test if the daughter can spin straw into gold or not and threatens her life doing so.
The miller's daughter goes well into the night without knowing what to do, when a dwarf-looking person walks into the room and asks what she would give him if he spun the straw into gold. She gives him her ring.
A few days later, Rumpelstiltskin offers to spin the straw into gold for the daughter's first child when she becomes queen. When the daughter becomes queen and has her first child, she begs for her child and Rumpelstiltskin says that if she could guess his name in three days, she could keep her child.
The first day, Rumpelstiltskin comes to the castle and the queen tries to guess his name, but fails. The second day, she tries to think up all the funny names she could think. She also failed that day.
The third and final day the queen sends out messengers to try and figure out Rumpelstiltskin's name. One messenger comes across a dwarf t a fire singing and doing a silly dance. The dwarf mentions his name and the messenger reports back. Rumpelstiltskin comes back to the castle and the queen guesses his name.