Essentially because of absorption. When photon flies into the material it interacts with its constituents. This interaction can be divided into two contributions. One of them is elastic and is the source of the index of refraction (because effectively it just slows the photon down) while the other one is inelastic. Photon gets absorbed by an atom (say) and later it is emitted as thermal radiation in random direction thereby losing the original information it carried.
When you look at this macroscopically, this process will be described by some parameter like penetration depth and intensity w.r.t. depth will decay exponentially. So if you made opaque objects thin enough, they would still be transparent (although the outgoing light would be weaker depending on thickness). Of course, this discussion completely avoids surface effects (reflection, refraction, scattering, etc.).
Note that all of this depends on frequency of the incident light. Atoms (let's just talk about them for simplicity; in reality there will be contribution also from molecules, lattice, free electrons and whatnot) have something called absorption spectrum. This arises because for certain frequencies electron can catch the photon and get excited to the higher energy state. So, while a material can be transparent in certain range of frequencies (like glass is for visible light) it can be quite opaque in others.