G-code is a text based set of commands. Like this:
M104 T0 S230
M104 is the command, T0 is the tool, and S230 is the parameter to set. In this case, I just told Extruder 0 to set the temperature to 230 degrees.
It is fairly standardized. However, the controller determines which codes go to what tool and what parameters to take.
In this case, this text string is sent to the controller (RAMPS in our example) over the USB serial connection. The code within the controller parses the string and then activates the appropriate output based on the tool, and moves it or sets it to the parameter.
So, in your laser cutter, you have X and Y steppers. So, you need movement g-codes, an axis for the tool, and the position as the parameter (G1 X100 = Move X axis 100mm). You also have a command to turn the laser on and off. The laser is the tool. A parameter of 0 is off and 1 is on.
But you don't really need to know this as that is all handled by the controller. And you use a program (like the slicer you use for your 3D printer) to convert the vectors or raster images into a set of G-Code commands. In our case, we use Inkscape and the G-Code plugin to do this for us.
The software G-Code generator does need to know some things about your controller. It needs to know what commands do what, what the names of the 'tools' are, and what parameters they expect. Again, though, all YOU really are involved in here is to use a matching software with a matching controller. You really do not need to know how it works.
As I mentioned, the hardest part is just changing one file in the source code for the controller to set it up for your machine. Things like steps/mm. This is how many pulses to the steppers are required to move the axis 1mm? You can either calculate that the hard way by pulley diameter, belt pitch, etc... or you can give it some default then command the movement 100mm, measure how much it actually moved, and use one of the numerous calculators on the net to find the real number. The Marlin firmware that goes in the RAMPS controller has the ability to set these things later. So you don't need to know it at the beginning and you can change these things whenever appropriate without going through all the firmware customization again. It stores this information within the controller memory.
Thankfully, for the K40 at least, someone else has already added the RAMPS to their K40. So they have already modified the firmware for you and set all this up. All you need to is upload it to the RAMPS and go.