This is due to the different numbers of electrons in each molecule and VSEPR (Valence Shell Electron Repulsion) theory. This theory states that as electrons are negatively charged, the valence electrons in different atoms in a molecule repel each other. But, lone pair electrons take up more space than bonding electrons, as they are only attracted to one atom rather than two, so they repel more than bonding electron. Therefore we can order repulsions between different types of electron pairs: lone pair-lone pair > bonding pair- lone pair > bonding pair - bonding pair.
The total number of valence electrons in CO2 is 4 from carbon, plus six from each oxygen = 16. The carbon is in the centre because it has lower electronegativity. If we only form single bonds from C-O, carbon does not form a stable octet of electrons so we need to from double bonds. O=C=O There are only bonding electrons around the carbon which repel equally so the molecule is linear. For H2O, the total number of valence electrons is 1 from each hydrogen plus 6 from oxygen = 8. We cannot put hydrogen in the centre because it can only hold two electrons, due to its principle quantum number of 1. Therefore oxygen goes in the centre. Forming single bonds to each hydrogen leaves two more pairs of electrons which go around the oxygen atom, to complete the octet. These are lone pairs. There are four pairs of electrons around the oxygen atom so it cannot be linear. It must be v-shaped! If each pair of electrons repelled equally it would be in a tetrahedral arrangement, with 109 degree bond angles. But lone pairs repel more than bonding pairs, compressing the bonding angle to 104.5 degrees.