What is the reason for the square shape of the cells on grid fins such as those on the Falcon 9 landed by SpaceX, on missiles, or other places?
There are 3 regular geometric patterns you can use: triangle, rectangle and hexagon. All others have irregularities (e.g. very small cells next to larger cells) or space lost (circles).
According to the 2004 study, 'Grid pattern effects on aerodynamic characteristics of grid fins':
The results of the wind tunnel test indicate that the aerodynamic characteristics of grid fin depend much more on the area of each and total grid cell than that of lifting surface. In other words, the triangle pattern could be better choice for the grid fin because of higher structural strength compared to a square and a hexagonal pattern. That makes it possible to have a thinner web, which reduces the drag of the grid fin.
The size of the cells is driven by the shockwaves that come off the cell walls when supersonic: you don't want the shockwave to hit the opposite cell wall. Cells with more corners have trouble in the transonic region (where the study found the hexagonal cells had choked flow, while the triangular and rectangular cells didn't).
Why SpaceX chose a rectangular grid is a more difficult question to answer, as SpaceX doesn't publish much of its research.