For publishing site it is recommended to hide “_layouts” pages from crawling by search engines. When you create SharePoint public facing site using “publishing site” template it activates the feature “ViewFormPagesLockDown”.
This feature prevents users from opening any of pages from “_layouts” folder. But this feature is not available in “team site”, “blank site” and other site template.
But you can prevent users from accessing application pages (_layouts) by activating this feature manually. This is also helpful preventing search engines from crawling application pages.
If you have anonymous access enabled before activating this feature, you need to disable anonymous access and then re-enable anonymous access again.
If you want to have some of the application pages to be visible by anonymous, you need to UnsecuredLayoutsPageBase class for such pages.