Electromagnetic and gravitational responses and anomalies in topological insulators and superconductors