Gaia Data Release 2. The astrometric solution