The electronic structure of the perovskite LaCoO3 for different spin states of Co ions was calculated in the local-density approximation LDA+U approach. The ground state is found to be a nonmagnetic insulator with Co ions in a low-spin state. Somewhat higher in energy, we find two intermediate-spin states followed by a high-spin state at significantly higher energy. The calculations show that Co 3d states of t2g symmetry form narrow bands which could easily localize, while eg orbitals, due to their strong hybridization with the oxygen 2p states, form a broad σ* band. With temperature variation which is simulated by a corresponding change of the lattice parameters, a transition from the low- to intermediate-spin state occurs. This intermediate-spin (occupation t52ge1g) can develop an orbital ordering which can account for the nonmetallic nature of LaCoO3 at 90 K<T<500 K. Possible explanations of the magnetic behavior and gradual insulator-metal transition are suggested.