How can Arduino know that the number in a variable is a pin number and not something else?
Yes, a variable is just somewhere to store some data.

It's what you do with the variable that is important.

For instance:

int ledpin = 13;
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, HIGH);


That will set the variable ledpin to 13, then the next line will query the ledpin variable to find out what number it is, then pass that number to the pinMode function to set the mode of the pin to OUTPUT. The line after then does the same thing but passes the number 13 that it read from ledpin to the digitalWrite function to turn the LED on.

However, if you do this:

int ledpin = 13;
pinMode(ledpin, OUTPUT);
ledpin = 10
digitalWrite(ledpin, HIGH);


then it will act just the same at first, but the digitalWrite function will get the number 10 not the number 13 since the value in ledpin has been changed to 10 in the third line.

Remember: an integer is just a number - it's only when you do something with that number that it has any meaning.