As said in my comment on the question itself: That settings page is the place to configure the default behaviour. In your described case, those settings define the screen to switch off after 30s of idle time (i.e. no touch event), plus activate the Keyguard action (i.e. the lock pattern, PIN, or password) after additional 5 seconds (so if you switch the screen back on within less than 5s after it switched off automatically, you do not have to unlock the device as it would not yet be locked).
Now why does the screen stay on for 20 minutes or more, though configured to switch off after 30s of idle time? The solution is not always that obvious. Apps with the appropriate permissions might override those default settings. A good example is a navigation app: One surely doesn't want to permanently touch the screen to keep it from switching off while navigating. Another example might be an ebook reader prolonging the timeout.
While a reboot might solve that issue once it happens (in case a quick-fix is needed), in the long run it will prove better to check running apps for candidates overriding the mentioned default settings. While the navigation app might be obvious (and most likely desired), other apps might let you configure this behaviour.
In case you cannot easily figure out which app is causing it, a look at the system logs might prove useful. aLogcat is one example of apps helping you to capture log contents (there are many others as well). It might produce a large amount of lines, but searching the content for strings like "screen", "timeout", and the like has good chances to narrow it down.