That depends. The answer is as long as it reasonably takes police to conduct the investigation. For example, if you’ve been stopped for speeding, police can technically only detain you long enough to check your paperwork and write you a speeding ticket. However, if the officer discovers evidence of other criminal activity during the stop, they may legally prolong the investigation. Every police encounter is different and the specific circumstances will always determine what it considered a reasonable detention.
If you choose to challenge a detention, your lawyer will have to argue that police kept you longer than necessary under the circumstances. If the judge agrees, any criminal charges that resulted from your detention will likely be dropped.
Remember that unless you verbally ask to leave, courts will most likely regard the detention as voluntary. So any time police detain you, it’s a good idea to ask if you’re free to go. If the officer says you may leave, it’s up to you to leave the scene of the encounter. If you choose to stay, the detention is automatically legal.