Shape of Earth's magnetic field Why earth's magnetic field is compressed on day side and extended like tail on night side, like this

I know that it's due to solar wind but not how that compression and extension happens.
 A: Honeste painted an excellent qualitative picture, but here’s a more mathematical answer:  
First of all, note that the distorted magnetic field is the sum of a dipole field created by currents in the earth’s core (essentially unaffected by the solar wind) and a further contribution from hard-to-describe currents induced in the wind itself.  
The plasma in the solar wind is a conductive fluid.  When a conductive fluid moves through a magnetic field, the field induces currents, which distort the field, while reactive $J\times B$ forces affect the fluid flow.   You could even say that in the limit of infinite conductivity, the field lines would be frozen into the fluid (i.e., the fluid’s velocity would be parallel to the field lines).  The picture shows clearly that the field lines run parallel to the wind’s flow in the tail.  
The MHD “induction equation” used to calculate the distorted field is $$\dot{B}=(B\cdot \nabla )V-(V\cdot \nabla )B+\tfrac{1}{4\pi \sigma }{{\nabla }^{2}}B$$  To derive it, throw $J=\sigma (E+V\times B)$, $curl(E)=-\dot{B}$, and $curl(B)=4\pi J+o({{c}^{-2}})$ into a cauldron, don a wizard’s pointed hat, and stir.    
A: The Earth's magnetosphere is formed by the internal magnetic fields generated by a dynamo.  The global field geometry is roughly that of a magnetic dipole.  Were it not for the solar wind, the Earth's field would look like a rotating dipole.

Why earth's magnetic field is compressed on day side and extended like tail on night side, like this

The asymmetry between the dayside(sunward side) and nightside(anti-sunward side) of the magnetosphere results from the dynamic and magnetic pressure exerted on the magnetosphere by the supersonic, magnetized solar wind plasma.  The compression and expansion of the dayside and nightside, respectively, magnetic fields occurs in a similar way to that of flow lines in wind tunnel experiments with blunt obstacles like spheres.

I know that it's due to solar wind but not how that compression and extension happens.

There is an additional complication arising from the presence of magnetic fields and a plasma called magnetic reconnection.  That is, the magnetic fields reconfigure on the dayside and drape around to the nightside where they again reconfigure.  The new topology in the nightside experiences an unbalanced magnetic tension and "relaxes" to less strained state, i.e., the fields become more dipolar which is viewed in simulations as the stretched fields becoming less stretched.  This process is dynamic and continuously evolving.  The stretched nightside part of the magnetosphere is called the magnetotail and its relaxation results in processes that lead to the aurora.
