You accumulate credits based on your earnings. In 2016, you get one credit for every $1,260 you earn (that figure is adjusted higher each year), up to a limit of four credits per year. These credits remain on your record even if you change jobs or stop working for a while.
If you were born after 1929, you need 40 credits in order to receive Social Security retirement benefits.