Diagram of a nitrogen atom
Inside a red giant star, the star smashes lighter atoms together and gets energy to burn, and also has heavier atoms left over. Nitrogen is one of these heavier atoms, left over when a star smashes together carbon atoms. Nitrogen is not very heavy, though, and a lot of stars make a lot of nitrogen. When the red giant star explodes into a supernova, the nitrogen inside it goes shooting off into space, where it becomes part of a nebula, or cloud of space dust. All of the nitrogen in the universe comes from inside stars.
Most of the nitrogen in the universe stays in these nebulae, usually joining up with other nitrogen atoms to make molecules of two nitrogen atoms. Nitrogen atoms have five electrons in their outer ring, but they have room for eight electrons there. Often they join up with another nitrogen atom, each sharing electrons to fill up the rings. Because these nitrogen molecules are very stable, they don't break apart and join up with other kinds of atoms very much. It takes a lot of energy to break up a nitrogen molecule. But some of the nitrogen in space did become part of larger hydrocarbon molecules called amino acids.
When the Earth formed, about four and a half billion years ago, some nitrogen fell into the Earth's gravity and became the main ingredient in Earth's air - more than three-quarters of Earth's air is made of nitrogen molecules.
When life first evolved on Earth, about three and a half billion years ago, the cells used nitrogen to make protein molecules, which are part of what cells are made of, and to make RNA and DNA, the genetic material that allows cells to reproduce. When the bacteria that used this nitrogen died, the nitrogen decayed out of their bodies and soon more complicated plants evolved that could use this nitrogen too. Plants use nitrogen just like bacteria do, to make amino acids and proteins. Animals, including people, need nitrogen for the same reasons. We get our nitrogen by eating plants like carrots or lettuce, or by eating other animals, like pigs, that ate plants.
To find out more about atoms, check out these books from Amazon.com or from your library: