We pick some starting point assumption (say, there is no relationship between two variables). We then gather some data with measurements of independent and dependent variables. And then we ask ourselves, what % of the time will we see a relationship of this magnitude or greater by random chance?
One way to go about calculating the p-value would be by simulation. Another would be to use a mathematical formula to jump straight to this number.