Extending @Howdy_McGee answer.
- First you can not have two post with the same slug (Not title) which explained already in other answers.
- Second you can have page and post with same slug. But you will be able to access only page when your permalink structure is
/%postname%/. And there is debate going on this ticket#13459 about this issue/feature since 6 years :P.
When a request is made WordPress extract the request URI and match with the permalink structure in function parse_request(). If a match is found then query_vars are filled and this information is passed to WP_Query class where actual SQL is prepared and perform to display the result.
In case of page If it is page WordPress uses get_page_by_path and retrieve the ID of page to use in SQL query.
In case of post If it is a post then WordPress uses slug of post in SQL.
In short WordPress does not required post ID (Not in page) when permalink structure is set to /%postname%/ it uses the post slug to query database.
Therefore, with same slug of post/page a page is displayed first because with two matches WP check for page request first.