Origin of weak-link behavior of grain boundaries in superconducting cuprates and pnictides