Carbon (C) belongs to Group IV A in the periodic table.
Atomic number of C = 6
Electron configuration =

Oxygen (O) belongs to group VI A in the periodic table
Atomic number of O = 8
Electron configuration =

Based on the electron configurations, the valence shell in C and O have 4 and 6 electrons respectively. As a result carbon is tetravalent, and in CO2 this is satisfied as the C atom forms a double bond with each O atom. Hence the structural formula is:
O=C=O
In addition, there are two lone pairs of electrons on each of the O atoms.