Torture was around long before the midle ages. People were routinely tortured in ancient Rome, for instance slaves could only give evidence in court under torture, because it was assumed that their word was not to be trusted.
Torture was used in the medieval and early modern eras to extract confessions from criminals. it was used on order of the judicial establishment, rulers, etc.