There is a general rule that if a mussel does not open easily after it is cooked, it should be thrown away. The thinking is that mussels that don't open were dead before they were cooked, and bacteria in the dead mussels could cause food poisoning. This is a myth. Mussels that have been thoroughly cooked are perfectly safe to eat.
When you buy fresh mussels, you should try to get them home as quickly as possible to keep them alive until it's time to cook them. To keep them fresh, add a few tablespoons of salt to a large pot of water. Remove the mussels from any plastic bags or packaging. Put them into the pot and cover them with more water. Store them in the fridge; if there is no room, regularly add ice or store them in a cool place.
Clean the mussels thoroughly with a stiff-bristled brush to remove any barnacles from their shells before you cook them. Make sure all mussel shells are tightly closed before cooking them as well, as this means the mussels are still alive.
Even if a few of your mussels die before you cook them, the meat should be heated enough in the cooking process to kill any bacteria and render them safe to eat. When mussels are cooked, the heat disintegrates their abductor muscles, which keep the shells closed, making them easy to open and eat. However, sometimes the muscles don't disintegrate, which keeps a perfectly good mussel from easily opening.
While cooking mussels properly at high temperatures should make them sage, there is an exception. If a raw mussel is open and doesn't snap shut when it is lightly tapped, it means they it has been dead long enough for its muscles to stop working. Mussels like these are not safe to eat and should be discarded immediately.