Finding the nearest polygon
If shape and number of polygons do not change (or at least not often), I would add an extra step (the new first step) to your algorithm.

Calculate the center and radius of the smallest surrounding circle for each polygon. This way you can calculate in a minimal and maximal distance of all vertices from the given position in one run without iterating through them.

If the minimum distance for a polygon is greater than the maximum of an other polygon, no vertex of that polygon can be the nearest => we can ignore this polygon.

Than proceed like you did before. 


calculate distance to each polygon surrounding circle and eliminate all polygons which are too far away to be a match
Entity: performs a loop through all not eleminated polygons.
Polygon Loop: loops through ALL vertices of the polygon in the current iteration.
Vertex Loop: if the distance between the entity and the current vertex is lower than the distance between the entity and the previous vertex, save the current vertex and the ID of the polygon the vertex belongs to.
Once all loops are finished, take the nearest polygon/vertex and get the vertex of the polygon to the left and right of the nearest vertex.
Check the left and right vertices to see which is closest to the nearest vertex. Doing so, finds the nearest line of collision to the entity.