For 404 page, for not found files or items, Sitecore has out-of-box setting -

    <setting name="RequestErrors.UseServerSideRedirect" value="true" />

Make sure, you define your physical 404 page in below setting -

    <setting name="ItemNotFoundUrl" value="/sitecore/service/notfound.aspx" />

If you have the 404 in Sitecore content tree, you can refer blog - http://sitecoreblog.patelyogesh.in/2013/06/handle-404-page-not-found-in-sitecore.html

For 500, you can do below configurations in web.config file -

    <customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx">
      <error statusCode="404" redirect="~/404.aspx"/>
      <error statusCode="500" redirect="~/500.aspx"/>
    </customErrors>