Why a low-pass filter applied to a square wave gives a weird wave?
There are many types of low pass filter.

The one that gives your 'weird waveform' is the simplest 1st order RC filter. This passes the fundamental signal. However, it not only passes all the higher harmonics, but also gives them a phase shift.

The frequency response of the perfect lowpass filter shown in your image is actually not realisable as a physical filter. One problem is that the infinitely steep transition between passband and stopband requires an infinitely long impulse response, which means that you put a signal into the filter, and the delay is such that you never see it come out!

To this end, we approximate the filter with a shorter, usable impulse response. There are physical component filters, which must be causal (no output occurs before input), and signal processing filters, which can be acausal (in theory, output can occur before input, but any real system that uses them makes them causal by applying a delay).