How does selection sort sort? I've found the following algorithm for selection sort on the internet. 

program SelectionSort(input,output);
var
  A : Array[1..80] of Integer;
  n, i, j, k, m, loc, temp : Integer;
begin
  writeln('size of the array?');
  read(n);

  for i := 1 to n do
   read(A[i]);

  for k := 1 to n - 1 do
   begin
    m := A[k];
    loc := k;
     for j := k + 1 to n do
      begin
       if m &gt; A[j] then
        begin
         m := A[j];
         loc := A[j];
         loc := j;
        end;
      end;
     temp := A[k];
     A[k] := A[loc];
     A[loc] := temp;
   end;

  for i := 1 to n do
   begin
    writeln(A[i] : 3);
   end;

  readln();
  readln();
end.


I'd like to gain a better understanding of how it actually does the sorting. Could someone please help me with this?
Neither the code you have nor the Wikipedia article highlight the concept of selection sort. Therefore, consider this (functional) pseudocode:

def selection-sort []         = []
|   selection-sort x::xs as l = 
      let m = min l
      in
        m::(selection-sort (delete m l))
      end;


Here the idea is more clear


    $\ \ $1. Select the minimum from the list and delete it.
  $\ \ $2. Sort the remainder recursively.
  $\ \ $3. Prepend the minimum.

 The same works the other way around with the maximum, obviously.


Iterative code on arrays works slightly differently -- but the idea remains the same.