The control of variables is in large part what makes an experiment scientific in the traditional sense. Two categories of variables that need to be controlled are internal variables and external variables. Internal variables typically consist of the variables being manipulated and measured. External variables are factors outside the scope of the experiment, such as a participant becoming sick and unable to attend.
Identifying the Variables
To control for variables, you need to first identify what they are. The internal variables are usually the independent variable (what you are manipulating) and the dependent variable (what you are measuring). Ideally, these should be the only internal variables present in the experiment; however certain experiments (such as those using human subjects) may have other variables such as age, weight, IQ or other factors that you can't alter. The same is true for external variables. You need to identify threats to the experiment from outside the experimental setting. External variables can be numerous and include things like weather, room lighting, temperature, time, location and even natural disasters.
Select the Key Variables to Control
Particularly with external variables, you likely do not have the budget, time or means to control for everything, and this is especially true if you conduct your experiment in a natural setting (like measuring trees in a forest). Internal variables are often easier to control. Even if you can't eliminate them (such as variations in the subjects' weights), you should measure and record them. Statistical analysis can sometimes compensate for these differences (called covariants). For external variables, determine the ones most likely to affect your experiment and attempt to control these as best as you can. Consider the current events happening that might affect the results (for example; your participants may be under a great deal of stress due to an external situation), the reliability and accuracy of the instruments you use, and how you plan for participants dropping out of the study (participant mortality).
Controlling Internal Variables
For true experiments, randomization is one of the best controls for internal variables. In this situation, "random" means that each subject has equal chance of being selected for the experimental group (receiving the treatment) or the control group (not receiving the treatment). It can be tricky to achieve true randomization in practice. For example; if you have a room full of participants and you decide that the left half of the room is the experimental group and the right half is the control group, you are not accounting for people who may sit on one side or the other deliberately (such as to be near friends, the window or the door). Many researchers use a random number table to help them select subjects in a truly random order.
Controling External Variables
External variables can be very difficult to control, particularly if the variable affects all your participants at once. External variables affect how well the results of the experiment can be applied to others (external validity). Therefore, care should be taken in how you select subjects. In human subjects research, if all the participants are student volunteers from an introductory psychology course, it may not be a representative sample. Even if you can't fully control an external variable, such as historical events, at least record them and report them with your findings to allow the reader and your peers to draw their own conclusions.