Mixing and mastering happen at different stages of the process.
Mixing happens after you have recorded all the parts in your composition and is essentially the process of combining the various tracks (for example the guitar, the vocal, the drums, the synths) in a way that best serves the song. At its most basic it would be simply making sure all the relative levels are correct. Beyond that you might “EQ” each track or apply other processing such as compression. You might also pan the tracks differently in the stereo field and apply effects such as reverb to them.
Mastering happens after the mix process and is normally carried out by a different person to give a fresh perspective. In mastering the “mix” file (the single audio track that is a composite of all the tracks and which is the final product of the mix process) is processed to sound as good as possible and also to sit well with other songs, so that it doesn’t sounds too quiet, dark or bright when compared to other songs it will sit beside (other tracks on an album for instance). It will also allow it to sound as close as possible to a commercial release in terms of level and tone.
They are both important parts of the process but its important to remember that mastering is little more than polishing a mix, so always put as much into making a mix sound perfect and never expect miracles from mastering!