They can be cause to an overexposure to ultraviolet light, including excess sunlight. Genes can also play a role on moles. No not all are dangerous. You should yearly check you'r body to see if there are some unusual looking spots that may turn into skin cancer.
Many things cause moles, most of which we cannot control. Not all moles are dangerous, most are benign. Most moles that are malignant(cancerous), are caused by too much sun exposure.
Sometimes moles just show up. It does not mean they are cancerous. Sunlight and tanning will bring moles out too. You should check your moles for any discoloration and shape change. If anything looks different, you should see your doctor.