Looking into your opening post, I get the impression you’re asking not about the equation of motion of the Lagrangian shown in the attached image, but how one derives that Lagrangian. Of all the responses to your post, at least one is addressing your question—that getting a Lagrangian is usually educated guesswork. And, of course, that is correct.
So, I'll give a try, too. You probably will remain with lots of questions unanswered, especially when coming to details, but hopefully, you will be less confused.
The best source I have found for your inquiry is Zee’s QFT in a nutshell, 2nd edition. On pp. 18, he attempts to take the reader from a silly but instructive example of what a field is to a more serious mathematical description. He presents a “modern view” which he calls the “Landau-Ginzburg view.”
I am most certain Zee is adapting a theory by Landau and Ginzburg for superconductivity to his pedagogical task. Anyway, Zee states that, under Landau-Ginzburg's view, one starts with the kind of symmetry he (she) wants to impose on the physical problem. So, for elementary particles, it is Lorentz invariance; for solid state, it is translational symmetry in the crystal lattice, etc. Then, says Zee, one must decide on a field that, under the selected symmetry, will transform in some desired way. In the case of your actual example, you choose a real scalar field $\phi$. The next step is to write down the action. That’s a step we usually take for granted. An action is a space-time integral on a Lagrangian; when we apply certain mathematical operations called variational methods, we derive the Euler-Lagrange equations, from them the equation of motion, etc.
Therefore, however one puts it, it is the Lagrangian one wishes to construct. Practicing physicists say constructing a Lagrangian is an art as much as science! Still, there are also rules to observe with a Lagrangian. For instance, we expect it to be a function of our field(s), but what about the field’s derivatives? Will the Lagrangian contain first-order, second-order of any order? Here again, Zee’s text comes to the rescue. “No more than two time derivatves,” he claims, “for we don’t know how to quantize such actions.”
I’ve written all that to provoke your appetite. Please, read the original Zee!
There is much more to the topic, of course. I cannot cover even a tiny fraction of them, for I don’t know that much myself. But there are numerous books that will answer many of your questions. In fact, it is good practice at that level of yours to get used to thumbing through advanced texts and read only the absolute essentials. For example, at that point, I might suggest that you just read pp. 79-82 from Peskin & Schroeder’s QFT book. You will find in their discussion why the ways the various fields in a Lagrangian that interact with each other cannot be infinite in number—-that would have been a disaster in our attempts to understand physical phenomena. But, please, don't fret about the heavy mathematics in the text. Just read the concepts between the equations!
I hope that will help a little. Enjoy your studies!