What is the function of E-coli bacteria in our gut?
Nonpathogenic E.coli are a component of the gut microbiome of humans and many other organisms. 

They are commensals, meaning that when they remain in the areas they have evolved to live in, and when they do not acquire virulence factors, they are benign. They live in our digestive tract and basically do nothing to harm us.

In fact, commensal microorganisms like E.coli can be considered part of the multicellular organisms innate immune system. They take up space on the exposed surfaces of internal organs such as the intestines and prevent the colonization of pathogenic strains of microorganisms. 

Along with the endothelial cells and mucous, commensals form the barrier defenses that are the first line of protection against pathogenic organisms. Basically they are the good neighbors that don't cause problems and they don't leave space for bad neighbors to move in.

Problems can occur if they gain access to areas that are normally sterile. If the intestine is perforated and E.coli gain access to the thoracic cavity, they can become an opportunistic pathogen, as they will not be interacting with the host in a way that can control their proliferation. They can also come in contact with cells that are not expressing the necessary proteins to protect them from the E.coli.

You can also end up with the situation where a pathogenic bacteria or a bacteriophage carrying a virulence factor can transfer that virulence factor to the commensal E.Coli, turning them pathogenic.

But for the most part E.coli are there to take up space that could otherwise be colonized by harmful bacteria.