As others have suggested in comments, you should really start by reading Programming in Lua. It will explain this and much more and is really a perfect place to start if you want to learn Lua.
Well then, as for what it does
Given a table like this
local tab = {first = 1, second = 2, third = 3}
the way you would usually iterate over all the key-value pairs in the table is like this
for key, value in pairs(tab) do
print(key .. ": " .. tostring(value))
end
This will loop over the three values in the table first = 1, second = 2, etc.
For each pair, key is set to the table key and value to its value. It then executes the code between do and end with those variables set.
So the example above will print the following:
first: 1
second: 2
third: 3
How does it work?
This is a bit more complex; Let's first of all see what pairs actually returns:
> t = {}
> print(pairs(t))
function: 68f18400 table: 0066b1d8 nil
The table it returns as its second argument is the same that we passed in.
The function that's returned by pairs is the next function, which, given a table and a key, returns the next key in the table, within an unknown order but without ever repeating keys.
You can easily confirm that on the command line.
> print(t)
table: 0066b1d8
> print(next)
function: 68f18400
Lua then turns the for loop into something like the following:
do
local f, state, iterator = next, tab, nil -- this is what's returned by pairs
while true do
local key, value = f(state, iterator)
if key == nil then break end
iterator = key
print(key, value) -- This is the body of our for loop
end
end